Initially, I started my UserProfile like this:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
verified = models.BooleanField()
mobile = models.CharField(max_length=32)
def __unicode__(self):
return self.user.email
Which works nicely along with AUTH_PROFILE_MODULE = 'accounts.UserProfile'
set in settings.py
.
However, I have two different kinds of users in my website, Individuals and Corporate, each having their own unique attributes. For instance, I would want my Individual users to have a single user only, hence having user = models.OneToOneField(User)
, and for Corporate I would want them to have multiple users related to the same profile, so I would have user = models.ForeignKey(User)
instead.
So I thought about segregating the model into two different models, IndivProfile
and CorpProfile
, both inheriting from UserProfile
while moving the model-specific attributes into the relevant sub-models. Seems like a good idea to me and would probably work, however I would not be able to specify AUTH_PROFILE_MODULE
this way since I'm having two user profiles that would be different for different users.
I also thought about doing it the other way around, having UserProfile
inherit from multiple classes (models), something like this:
class UserProfile(IndivProfile, CorpProfile):
# some field
def __unicode__(self):
return self.user.email
This way I would set AUTH_PROFILE_MODULE = 'accounts.UserProfile'
and solve its problem. But that doesn't look like it's going to work, since inheritance in python works from left to right and all the variables in IndivProfile
will be dominant.
Sure I can always have one single model with IndivProfile
and CorpProfile
variables all mixed in together and then I would use the required ones where necessary. But that is just doesn't look clean to me, I would rather have them segregated and use the appropriate model in the appropriate place.
Any suggestions of a clean way of doing this?
UserProfile
abstract and letIndivProfile
andCorpProfile
inherit fromUserProfile
. This is still not going to solve the issue withAUTH_PROFILE_MODULE
. Which one is it going to point at? – Pleaduser
being different for each type. Please read the post, I have already mentioned I can do it but I'm looking for a clean approach. – PleadForeignKey
field on the corporate profile. – Statuary