Jamgreen
Jamgreen

Reputation: 11039

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?

Upvotes: 0

Views: 171

Answers (1)

ekrah
ekrah

Reputation: 624

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.

Upvotes: 1

Related Questions