2

对于应用程序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)

笔记

  • 我已验证用户是组织的成员,在管理员和数据库中都是 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 ','散列',' init ',u'模块',' ne ',' new ',' reduce ',' reduce_ex ',' repr ',' setattr ',' setstate ','sizeof ',' str ','子类挂钩',' unicode', '弱引用', '_check_column_name_clashes', '_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together', '_check_local_fields', '_check_long_column_names', '_check_m2m_through_same_relationship', '_check_managers', '_check_model', '_check_model_name_db_lookup_clashes', '_check_ordering', '_check_swappable', '_check_unique_together', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_password', '_perform_date_checks', '_perform_unique_checks ','_save_parents','_save_table','_set_pk_val','_state','check','check_password','clean','clean_fields','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',' has_module_perms','has_perm','has_perms','has_usable_password','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','objects','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']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']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']“用户名”、“用户名验证器”、“验证唯一”]“用户名”、“用户名验证器”、“验证唯一”]

4

1 回答 1

3

原来orgsIn = loggedInPerson.organizations_organization是 reutning organizations.Organization.None,因为它是一个查询集。只是打电话.all()工作

orgsIn = loggedInPerson.organizations_organization.all()=>[<org1>.<org2>,…]

于 2018-02-18T21:33:07.177 回答