Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 234
Doc Avatar asked Dec 04 '15 16:12

Doc


People also ask

Should you create custom User model in Django?

Every new Django project should use a custom user model. The official Django documentation says it is “highly recommended” but I'll go a step further and say without hesitation: You are straight up crazy not to use a custom user model up front.


1 Answers

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.

like image 179
Chris Hawkes Avatar answered Oct 08 '22 10:10

Chris Hawkes