Shahriar Rahman Zahin
Shahriar Rahman Zahin

Reputation: 642

Implement one-to-many relation with one-to-one relation without circular dependency

I have two model, User & Company where a company may have multiple users and a single owner(which is also a user). A user can be associated with only one company. Now my question is, how am I supposed to maintain the Owner of any company? Because if I use any one-to-one relation field in Company model then it will give me circular-dependency error as I have foreign-key relation field in my User model.
Models are given below:

User model

class User(BaseMixin, AbstractUser, BlameableMixin):
    """Extends django user with extra optional fields."""

    role = models.ManyToManyField(Role)
    company = models.ForeignKey(
        Company,
        related_name='user_company_map',
        on_delete=models.SET_NULL,
        null=True,

    )
    designation = models.CharField(max_length=max_length_medium, blank=True, unique=False)
    mobile_no = models.CharField(max_length=max_length_medium, blank=False, unique=False)
    organization = models.CharField(max_length=max_length_medium, blank=False, unique=False)

Company model

class Company(BaseMixin, BlameableMixin):
    """Company detail."""

    name = models.CharField(max_length=max_length_large, blank=True, unique=False)
    code_name = models.CharField(max_length=max_length_large, blank=True, unique=True)
    domain = models.CharField(max_length=max_length_large, blank=True)

As shown on the code above I have a foreign key field on User model that connects my User model to Company model.

Now I would like to have a field that will help me to keep track of a user who is the owner of any specific company. It will be also very helpful if you provide the django-orm queries for getting the company object with its users and owner. I would also like to query a User with its related company and whether he/she is the owner of this company or not.

Note: I would like to have the owner field on Company model but if according to my requirements it should be in User model, I won't mind. I am just looking for a solution. Any suggestion is appreciated. Thanks

Upvotes: 1

Views: 78

Answers (1)

joel
joel

Reputation: 56

Django allows you to specify the to argument of the ForeignKey as a string (https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey), which would enable you to create the relationship within the Django ORM

class Company(BaseMixin, BlameableMixin):
    """Company detail."""

    name = models.CharField(max_length=max_length_large, blank=True, unique=False)
    code_name = models.CharField(max_length=max_length_large, blank=True, unique=True)
    domain = models.CharField(max_length=max_length_large, blank=True)
    owner = models.OneToOne("User", related_name="owner_of")

You can then access the owner directly from a company object company_object.owner. The company of a specific user is accessed through user_object.company. You can also access the company that a user owns through user_object.owner_of.

Upvotes: 1

Related Questions