Doc
Doc

Reputation: 5266

Is better extend or substitute Django User model?

I need to have some additional fields in my User model and i don't know which is the best path to follow.

My first idea is to create a new model with my fields, as explained here, but i want it to be created along the User model and it will be needed to be handled separately, and this could be potentially painful since my project will be heavily based on django-rest-framework

Another idea is create a new model extending AbstractUser, as done here, and setting it as AUTH_USER_MODEL, so i'll have to manage only one object at a time

Any advice? Are there any best practice for situations like this?

Upvotes: 5

Views: 451

Answers (2)

Ire
Ire

Reputation: 1361

It is better to use a OneToOne relationship to the User model in your own 'Profile' model.

  • This lets you use django's authentication and user manager
  • Your app is reusable in other projects
  • You will waste less time on the user logic

This can save trouble down the road if at some point different kinds of users are necessary, or if the project has to be reused (by yourself or someone else).

Django's doc advises against writing your own User model as well.

Upvotes: 3

Chris Hawkes
Chris Hawkes

Reputation: 12410

Extending the user model is discussed here. https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#auth-custom-user

From what I've seen typically, a user profile table is created with the additional fields, than you should use signals to listen for any new user object being created to create your user profile object with a foreign key relationship to the auth user. that way you are not rewriting anything that comes out of the box with django, but just extending the user profile fields into another table.

Upvotes: 3

Related Questions