[Django]-Django: Two way one-to-one relationship. Chick or egg?

4๐Ÿ‘

โœ…

If two models exist such that each requires the other, they should usually be combined into a single model.

In any case there is probably a better way to structure the classes. Personally I would construct a UserProfile class as follows:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    administrator = models.BooleanField()

or possibly:

class UserProfile(models.Model):
    user = models.OneToOneField(User)

class AdministratorProfile(UserProfile):
    # attributes

or even:

class UserProfile(models.Model):
    user = models.OneToOneField(User):

class AdministratorProfile(models.Model):
    profile = models.OneToOneField(UserProfile)

any of these methods should work.

0๐Ÿ‘

Iโ€™m not totally sure I fully understand the differences between Administrator, Account, and contrib.auth.User,

This feels like the is-a vs has-a discussion. It sounds like Administrator is a User (really, a UserProfile, for reasons peculiar to django), and Administrator has an Account. Accounts do not have anything, since they cannot both own and be owned.

This is like a bank account vs a bank customer. Bank customers own their accounts, not the other way around. On the other hand, bank accounts have a list, and that list is the bank customers that may use the account.

Leave a comment