对于应用程序django-organizations,您如何获得用户的组织?从文档中说
>>> from organizations.utils import create_organization
>>> chris = User.objects.get(username="chris")
>>> soundgarden = create_organization(chris, "Soundgarden", org_user_defaults={'is_admin': True})
>>> soundgarden.is_member(chris)
True
>>> soundgarden.is_admin(chris)
True
>>> soundgarden.owner.organization_user
<OrganizationUser: Chris Cornell>
>>> soundgarden.owner.organization_user.user
>>> <User: chris>
>>> audioslave = create_organization(chris, "Audioslave")
>>> tom = User.objects.get(username="tom")
>>> audioslave.add_user(tom, is_admin=True)
<OrganizationUser: Tom Morello>
在我的代码中,我可以轻松做到:
@login_required
def bandView(request, bandSlug):
loggedInUser = get_object_or_404(User, username=request.user.get_username())
organization_requested = get_object_or_404(Organization, slug=bandSlug)
if organization_requested.is_member(loggedInUser):
#User is a member of the organization
elze:
# Not in this band
但我现在正尝试另一种方式:
@login_required
def artistView(request):
loggedInUser = get_object_or_404(User, username=request.user.get_username())
#something like....
loggedInUser.organizations #would print [<band1>,<band2>]
#or add a function to the Organization[User] class
Organizations.all().filter(member=loggedInuser)
Organizations.getMembership(loggedInuser)
笔记
我已经在Admin和数据库中确认用户是OrganizationUser的组织成员
print loggedInPerson.organizations_organizationuser
-> organizations.OrganizationUser.None
print loggedInPerson.organizations_organization
-> organizations.Organization.None
print loggedInPerson.organizations_organization_set
->错误print dir(loggedInPerson)
->['DoesNotExist','EMAIL_FIELD','Meta','MultipleObjectsReturned','REQUIRED_FIELDS','USERNAME_FIELD','class','delattr','dict','doc','eq','format',' getattribute','hash','init',u'module','ne','new','reduce','reduce_ex','repr','setattr','setstate','sizeof','str ','subclasshook','unicode','weakref','_ check_column_name_clashes','_ check_field_name_clashes','_ check_fields','_ check_id_field','_ check_index_together','_ check_local_fields','_ check_long_column_s,'_ check_s '_check_model','_ check_model_name_db_lookup_clashes','_ check_ordering','_ check_swappable','_ check_unique_together','_ do_insert','_ do_update','_ get_FIELD_display','getget_next_or_pre _'_'________ ','_ password','_ perform_date_checks','_ perform_unique_checks','_ save_parents','_ save_table','_ set_pk_val','_ state','check','check_password','clean','clean_fie lds”,“ date_error_message”,“ date_joined”,“ delete”,“ email”,“ email_user”,“ emailaddress_set”,“ first_name”,“ from_db”,“ full_clean”,“ get_all_permissions”,“ get_deferred_fields”,“ get_email_field_name” ,'get_full_name','get_group_permissions','get_next_by_date_joined','get_previous_by_date_joined','get_session_auth_hash','get_short_name','get_username','groups','has_module_perms','has_perm','has_perm',' id','invoice_set','is_active','is_anonymous','is_authenticated','is_staff','is_superuser','job_set','last_login','last_name','logentry_set','natural_key','normalize_username' ,“对象”,“ organizations_organization”,“ organizations_organizationuser”,“ password”,“ pk”,“ pm_set”,“ prepare_database_save”,“ refresh_from_db”,“ save”,“ save_base”,“ serializable_value”,“ set_password”,“ set_unusable_password”,“ socialaccount_set”,“ unique_error_message”,“ user_permissions”,“ username”,“ username_validator”,“ validate_unique”]
最佳答案
事实证明orgsIn = loggedInPerson.organizations_organization
正在重新查询organizations.Organization.None
,因为它是一个查询集。只需调用.all()
即可orgsIn = loggedInPerson.organizations_organization.all()
=> [<org1>.<org2>,…]