简体   繁体   中英

Extend Django User model

I want the exact same behaviour as django.contrib.auth.User but I need to extend the model with some user preferences and profile specific fields.

I know I could eventually make two new models which could be

class Profile(models.Model):
    user = OneToOneField(User)
    age = SmallPositiveIntegerField()

and

class Preference(models.Model):
    user = OneToOneField(User)
    background_color = CharField(max_length=6)

I don't know if it's smart to separate these things.

But I want to retrieve the user preferences on each page and if I use this approach by extending the User model with an OneToOneField it costs another sql query for each time I get user.preference.background_color .

I think it might be overkill to substitute the User model (or what) so what can I do?

Will proxy models be the answer?

The way people (Two Scoops, for example) generally recommend doing this is by subclassing AbstractUser (or AbstractBaseUser if you want even fewer of the base user fields; baseuser just gives you password, login timestamp, and is_active). You'd then just add fields as you would for a normal model and query them as you would for a normal model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM