Django: extending user model vs creating user profile model

Question:

I’m creating an app in Django and so far I have been using an extended user model like so:

class MyUser(AbstractBaseUser):
...

with all the user and profile info, but I see a lot of people creating different models for the profile and the user itself on stack overflow, using OneToOneField, although those are mostly old questions.
My question is: which is better and, if there isn’t a best among them, what are the advantages for each solution?
thanks!

Asked By: dietbacon

||

Answers:

A ForeignKey is to create a one-to-many relationship. In other words, it will return a queryset. For example, a single car has many wheels, but one wheel isn’t attached to several different cars.

A OneToOneField will create a relationship between strictly two objects. For example, the rim belongs to the front-left tire, and only that tire has that rim.

Does that make sense?

Answered By: jph

It depends on what you want to do — if you’re happy with the User model as it stands in the latest version of Django you should just use that — it’s easy and you’ll get a lot functionality that goes along with it — for example a pretty good permission system, and you can be sure to be compatible with all third party modules. But if you thing you’ll need to expand on the User model, it’s pretty straightforward how to do it. You might find that in the future you need to add more methods to your model than you expected.

The examples that you see with separate UserProfile / User model are mostly a legacy of django < 1.5, where that was the recommended way to extend the User model. There’s no reason to follow that pattern any more — it’s a lot more work to have to use two models where you just want one model


**2019 Update**

If you are starting a new Django project, you should always create your own custom user model that inherits from AbstractUser, as per the Django documentation, i.e.

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass 

even if you don’t need any additional functionality. The reason for this is that for very low effort, you are making it easy to customize your user object in the future. It’s very laborious to replace the built-in User object with your own after you have run the initial migrations, unless you’re able to delete all of your data and migrations and start over.

Answered By: Mark Chackerian

I find some useful information in Django docs:

Extending Django’s default User¶

If you’re entirely happy with Django’s User model and you just want to
add some additional profile information, you could simply subclass
django.contrib.auth.models.AbstractUser and add your custom profile
fields, although we’d recommend a separate model as described in the
“Model design considerations” note of Specifying a custom User model.
AbstractUser provides the full implementation of the default User as
an abstract model.

And:

Model design considerations

Think carefully before handling information not directly related to
authentication in your custom User Model.

It may be better to store app-specific user information in a model
that has a relation with the User model. That allows each app to
specify its own user data requirements without risking conflicts with
other apps. On the other hand, queries to retrieve this related
information will involve a database join, which may have an effect on
performance.

So if I reads it correctly, it means if the fields are related to authentication, then you should consider substitute the original User model. But if it’s not related, like profile fields, such birthday, or profile_image, then you might want to create a standalone app that reference the original User model.

And a good tutorial I found: http://riceball.com/d/content/django-18-tutoria-52-adding-user-profile

Answered By: Hangfei Lin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.