Get User's organization(s) in django-organizations

1.9k Views Asked by At

For the app django-organizations, how do you get a User's organization? From the docs it says

>>> 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>

and in my code I can easily do :

@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

but I am trying to work the other way now:

@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)

Notes

  • I have verified the user is a member of the organization as an OrganizationUser in both Admin and database
  • print loggedInPerson.organizations_organizationuser -> organizations.OrganizationUser.None
  • print loggedInPerson.organizations_organization -> organizations.Organization.None
  • print loggedInPerson.organizations_organization_set -> errors
  • 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_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', 'groups', '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']

1

There are 1 best solutions below

1
On

turns out orgsIn = loggedInPerson.organizations_organization was reutning organizations.Organization.None as it is a queryset. just calling .all() works

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