获取django组织中的用户组织

时间:2018-02-18 14:44:32

标签: python django django-organizations

对于应用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 - &gt; organizations.OrganizationUser.None
  • print loggedInPerson.organizations_organization - &gt; organizations.Organization.None
  • print loggedInPerson.organizations_organization_set - &gt;错误
  • print dir(loggedInPerson) - &gt;

    [&#39; DoNotExist&#39;,&#39; EMAIL_FIELD&#39;,&#39; Meta&#39;,&#39; MultipleObjectsReturned&#39;,&#39; REQUIRED_FIELDS&#39;,& #39; USERNAME_FIELD&#39;,&#39; &#39;,&#39; delattr &#39;,&#39; dict &#39;,&#39; doc &#39;,&#39; eq &#39;,&#39; 格式&#39;,&#39; getattribute &#39;,&#39; 哈希&#39;,&#39; init &#39;,u&#39; 模块&#39;,&#39; ne &#39;,&#39; < strong> new &#39;,&#39; reduce &#39;,&#39; reduce_ex &#39;,&#39; repr &#39;,&#39; setattr &#39;,&#39; 设置状态&#39;,&#39; ; sizeof &#39;,&#39; str &#39;,&#39; 子类别&#39;,&# 39; unicode &#39;,&#39; weakref &#39;,&#39; _check_column_name_clashes&#39;,&#39; _check_field_name_clashes&#39;, &#39 ; _check_fields&#39;,&#39; _check_id_field&#39;,&#39; _check_index_together&#39;,&#39; _check_local_fields&#39;,&#39; _check_long_column_names&#39;,&#39; _check_m2m_through_same_relationship&#39; ,&#39; _check_managers&#39;,&#39; _check_model&#39;,&#39; _check_model_name_db_lookup_clashes&#39;,&#39; _check_ordering&#39;,&#39; _check_swappable&#39;,&#39; _check_unique_together&#39;,&#39; _do_insert&#39;,&#39; _do_update&#39;,&#39; _get_FIELD_display&#39;,&#39; _get_next_or_previous_by_FIELD&#39;,&#39; _get_next_or_previous_in_order&#39;, &#39; _get_pk_val&#39;,&#39; _get_unique_checks&#39;,&#39; _meta&#39;,&#39; _password&#39;,&#39; _perform_date_checks&#39;,&#39; _perform_unique_checks& #39;,&#39; _save_parents&#39;,&#39; _save_table&#39;,&#39; _set_pk_val&#39;,&#39; _state&#39;,&#39; check&#39;,& #39; check_password&#39;,&#39; clean&#39;,&#39; clean_fields&#39;,&#39; date_error_message&#39;,&#39; date_joined&#39;,&#39;删除&# 39;,&#39;电子邮件&#39;,&#39; email_user&#39;,&#39; emailaddress_s et&#39;,&#39; first_name&#39;,&#39; from_db&#39;,&#39; full_clean&#39;,&#39; get_all_permissions&#39;,&#39; get_deferred_fields&#39;, &#39; get_email_field_name&#39;,&#39; get_full_name&#39;,&#39; get_group_permissions&#39;,&#39; get_next_by_date_joined&#39;,&#39; get_previous_by_date_joined&#39;,&#39; get_session_auth_hash& #39;,&#39; get_short_name&#39;,&#39; get_username&#39;,&#39; groups&#39;,&#39; has_module_perms&#39;,&#39; has_perm&#39;,& #39; has_perms&#39;,&#39; has_usable_password&#39;,&#39; id&#39;,&#39; invoice_set&#39;,&#39; is_active&#39;,&#39; is_anonymous&# 39;,&#39; is_authenticated&#39;,&#39; is_staff&#39;,&#39; is_superuser&#39;,&#39; job_set&#39;,&#39; last_login&#39;,&# 39; last_name&#39;,&#39; logentry_set&#39;,&#39; natural_key&#39;,&#39; normalize_username&#39;,&#39; objects&#39;,&#39; organizations_organization&#39 ;,&#39; organizations_organizationuser&#39;,&#39;密码&#39;,&#39; pk&#39;,&#39; pm_set&#39;,&#39; prepare_database_save&#39;,&#39; ; refresh_from_db&#39 ;,&#39;保存&#39;,&#39; save_base&#39;,&#39; serializable_value&#39;,&#39; set_password&#39;,&#39; set_unusable_password&#39;,&#39; ; socialaccount_set&#39;,&#39; unique_error_message&#39;,&#39; user_permissions&#39;,&#39;用户名&#39;,&#39; username_validator&#39;,&#39; validate_unique&#39; ]

1 个答案:

答案 0 :(得分:3)

结果orgsIn = loggedInPerson.organizations_organization正在重新审核organizations.Organization.None,因为它是一个查询集。只是致电.all()工作

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