django-organizations icon indicating copy to clipboard operation
django-organizations copied to clipboard

RelatedObjectDoesNotExist: Organization has no owner

Open danihodovic opened this issue 5 years ago • 3 comments
trafficstars

I'm using the helper function to create a default organization when a user signs up.

https://github.com/bennylope/django-organizations/blob/c6ead20907ca08e1cceecaf96a109ccddc1187eb/organizations/utils.py#L55-L63

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

danihodovic avatar Jan 19 '20 21:01 danihodovic

That definitely does not sound right.

https://github.com/bennylope/django-organizations/blob/c6ead20907ca08e1cceecaf96a109ccddc1187eb/tests/test_utils.py#L22-L38

That function creates the owner right at the end.

https://github.com/bennylope/django-organizations/blob/c6ead20907ca08e1cceecaf96a109ccddc1187eb/organizations/utils.py#L109-L112

Before I dig further, could you share how you're calling that function and what models you're using?

bennylope avatar Jan 20 '20 01:01 bennylope

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)

danihodovic avatar Jan 20 '20 11:01 danihodovic

Please check my comment here

harikvpy avatar Nov 28 '20 07:11 harikvpy