How do I remove the circular dependency in my Organization-Owner-Member models?
<p>Heyo, I'm fairly new to this stuff so please pardon me if this is a stupid question. 
I'm trying to create an app where users can create organizations and join already existing ones. My requirements are:</p>

<ul>
<li>an organization may have only one user designated the owner (the user who creates it)</li>
<li>users must be able to join several organizations</li>
<li>users must be able to create organizations and therefore be owners of multiple organizations</li>
</ul>

<p>So far I've got the following models.py:</p>

<pre><code>from django.db import models
from django.utils.translation import ugettext_lazy as _

from myapp.users.models import User


class TimeStampedModel(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class Org(TimeStampedModel):

    name = models.CharField(
        _('Organization name'),
        max_length=255,
    )
    owner = models.OneToOneField('OrgOwner')
    members = models.ManyToManyField(
        'OrgMember',
        related_name='organization_members'
    )
    users = models.ManyToManyField(
        User,
        related_name='organization_users'
    )

    def __str__(self):
        return self.name


class OrgMember(TimeStampedModel):

    user = models.ForeignKey(
        User,
        related_name='user_profile'
    )
    organization = models.ForeignKey(
        Org,
        related_name='member_organization'
    )

    def __str__(self):
        return self.user.username


class OrgOwner(TimeStampedModel):

    member = models.OneToOneField(OrgMember)
    organization = models.OneToOneField(Org)

    def __str__(self):
        return self.member.__str__()
</code></pre>

<p>My issue is that the way I've designed the models so far, I have a circular dependency. In order for a user to create an Org from scratch he needs to be an OrgMember of said, not yet created, Org. In other words, I cannot instantiate an OrgMember without assigning it an Org, but I cannot instantiate a new Org without an OrgOwner, for which I also need an OrgMember. </p>

<p>I'm sure there is just an error in my reasoning here. But perhaps there are some best practices for situations like this one you could share. There is probably a fairly simple solution to this, but I haven't been able to find one in an hour of searching and reading the django docs. Any help is much appreciated!</p>

<p>I still can't see why you have so much looping information. Your <code>OrgMember</code> class has an organization field, even though <code>Org</code> already has a <code>ManyToMany</code> with <code>OrgMember</code>. Same thing with <code>OrgOwner</code> - except that points to <code>OrgMember</code> which already points to an <code>Org</code> as well. Both of those classes only have two fields - the <code>User</code> associated with it, and then the <code>Org</code>. Unless there are more fields, they serve no purpose - the same thing can be accomplished in one model:</p>

<pre><code>class Org(TimeStampedModel):
    name = models.CharField(_('Organization name'), max_length=255)
    # Only one owner, but each User can own more than one org
    owner = models.ForeignKey('auth.User', related_name='owned_orgs')
    members = models.ManyToManyField('auth.User', related_name='organization_members')
    users = models.ManyToManyField(User, related_name='organization_users')

    def __str__(self):
        return self.name
</code></pre>

<p>I'd recommend that you do some more reading <a href="https://docs.djangoproject.com/en/1.10/ref/models/fields/#module-django.db.models.fields.related" rel="nofollow">about Django's relationship fields</a></p>

python django django-models