OneToOne field versus model inheritance

30 views
Skip to first unread message

Paul Royik

unread,
Jan 20, 2015, 7:52:38 AM1/20/15
to django...@googlegroups.com
I have three models: Person, Client, Member
Person is a base model, Client and Member are profiles for Person.

class Person(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name=_('email address'),
        max_length=255,
        unique=True,
    )


class Client(User): #or maybe models.Model and explicit OneToField 
    first_name = models.CharField(verbose_name=_('first name'), max_length=30)
    last_name = models.CharField(verbose_name=_('last name'), max_length=30)

class Member(User): #or maybe models.Model and explicit OneToField 
    description = models.CharField(verbose_name=_('first name'), max_length=255)
   # other stuff


So, what I want?
1. In admin, when we add client or member, I want to fill field email (fields from base class) as if it was in derived class. For example, in admin list_display = ('email', 'first_name'). Not select boxes for user.
2. Person class can be instantiated separately and the "attached" to the created profiel. Like person=Person(email="te...@gmail.com"), client = Client(person=person,first_name="test"...). Especially, this should work in forms. When user (person) is authenticated I want to give ability to fill in profile (client) form and attach person to this form.
3. One person can have both accounts. If I delete client/member, corresponding person should not be deleted.

3rd option actually is not necessary, but it is useful for me to know how to do it.

So, option 1 is perfectly solved by inheritance, Person is User, but this approach fails when option 2 is implemented. Since Person and Client is considered as one whole, I can't attach user, duplicate key error.
Option 2 is resolved by extending models.Model and appending person=models.OnetoOneField(Person,primary_key=True). However, admin is broken (1st option), because Client don't have fields like email.


So, what approach to take in order to solve above issues?
Are there simple ways?
If there are no simple ways, is there advanced way, like overriding metaclass, object descriptors or writing custom OneToOne field?

Any suggestions are welcomed.

Thank you. 

Collin Anderson

unread,
Jan 22, 2015, 8:12:12 PM1/22/15
to django...@googlegroups.com
Hi,

Django doesn't really provide a way to create a subclass from an already existing object in the admin. It's actually pretty hard to do even in the code. So for that reason I'd recommend the non subclass with the OneToOneField.

Actually, if I were doing it, this is what I would do:

class User(AbstractBaseUser):
    is_client
= models.BooleanField(default=False)
    is_member
= models.BooleanField(default=False)
    description
= models.CharField(verbose_name=_('first name'), max_length=255, blank=True)
   
# include both client and member fields here and have them be blank=True

Collin
Reply all
Reply to author
Forward
0 new messages