RelatedObjectDoesNotExist: Organization has no owner
danihodovic opened this issue · comments
I'm using the helper function to create a default organization when a user signs up.
django-organizations/organizations/utils.py
Lines 55 to 63 in c6ead20
The docstring says:
Returns a new organization, also creating an initial organization user who is the owner.
However the first user doesn't in fact seem to be the organization owner:
>>> org.is_owner(user)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dani/repos/ninjads/.venv/lib/python3.7/site-packages/organizations/abstract.py", line 181, in is_owner
return self.owner.organization_user.user == user
File "/home/dani/repos/ninjads/.venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 423, in __get__
self.related.get_accessor_name()
ninjads.organizations.models.Organization.owner.RelatedObjectDoesNotExist: Organization has no owner.
Organization has no owner.
The member and admin checks succeed:
>>> org.is_admin(user)
True
>>> org.is_member(user)
True
That definitely does not sound right.
django-organizations/tests/test_utils.py
Lines 22 to 38 in c6ead20
That function creates the owner right at the end.
django-organizations/organizations/utils.py
Lines 109 to 112 in c6ead20
Before I dig further, could you share how you're calling that function and what models you're using?
Organization models
from hashid_field import HashidAutoField
from organizations.abstract import (
AbstractOrganization,
AbstractOrganizationOwner,
AbstractOrganizationUser,
)
class Organization(AbstractOrganization):
id = HashidAutoField(primary_key=True)
class OrganizationUser(AbstractOrganizationUser):
pass
class OrganizationOwner(AbstractOrganizationOwner):
pass
Organization creation on user sign up
import logging
from allauth.account.signals import user_signed_up
from django.dispatch import receiver
from organizations.utils import create_organization
from ninjads.organizations.models import Organization
logger = logging.LoggerAdapter(logging.getLogger(), extra={})
@receiver(user_signed_up)
def create_default_organization(
request, user, **kwargs
): # pylint: disable=unused-argument
logger.info("User %s signed up, creating a default organization", user)
create_organization(user, user.email, is_active=True, model=Organization)
Please check my comment here