Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - Multiple User Profiles
    primarykey
    data
    text
    <p>Initially, I started my UserProfile like this:</p> <pre><code>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 </code></pre> <p>Which works nicely along with <code>AUTH_PROFILE_MODULE = 'accounts.UserProfile'</code> set in <code>settings.py</code>. </p> <p>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 <code>user = models.OneToOneField(User)</code>, and for Corporate I would want them to have multiple users related to the same profile, so I would have <code>user = models.ForeignKey(User)</code> instead.</p> <p>So I thought about segregating the model into two different models, <code>IndivProfile</code> and <code>CorpProfile</code>, both inheriting from <code>UserProfile</code> 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 <code>AUTH_PROFILE_MODULE</code> this way since I'm having two user profiles that would be different for different users.</p> <p>I also thought about doing it the other way around, having <code>UserProfile</code> inherit from multiple classes (models), something like this:</p> <pre><code>class UserProfile(IndivProfile, CorpProfile): # some field def __unicode__(self): return self.user.email </code></pre> <p>This way I would set <code>AUTH_PROFILE_MODULE = 'accounts.UserProfile'</code> 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 <code>IndivProfile</code> will be dominant.</p> <p>Sure I can always have one single model with <code>IndivProfile</code> and <code>CorpProfile</code> 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.</p> <p>Any suggestions of a <strong>clean</strong> way of doing this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload