Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom fields to auth_user table in Django

Tags:

django

Currently I created another class/table called MyAppUser with my custom columns (such as address and phone number) that has a foreign key to Django's authentication User.

Something like this

from django.db import models
from django.contrib.auth.models import User

class MyAppUser( models.Model ) :
    def __unicode__( self ) :
       return self.user.username

    user    = models.ForeignKey( User )
    comment = models.TextField( blank = True )
    phone   = models.CharField( max_length = 135, blank = True )

Is the above method a good practice? Or should I try to modify the auth_user directly? And if so, how would I do that?

Bonus question: Is there a way to have both my custom table to be called User as well? I thought about trying from django.contrib.auth import models, then calling models.User, but then I think that would conflict with django.db.models as well. Maybe I need to try from django.contrib.auth.models import User as AuthUser? Is this a good idea?

like image 469
hobbes3 Avatar asked Mar 12 '12 16:03

hobbes3


People also ask

Can you add fields to Django user model?

The user detail view will have username, password, first name, last name, email address, active, staff status, superuser status, groups, user permissions, last login, and date joined. To add your new fields to the detail view, you'll need to use fieldsets.

What is Auth_user_model in Django?

Django allows you to override the default user model by providing a value for the AUTH_USER_MODEL setting that references a custom model. Method 2 – AUTH_USER_MODEL : AUTH_USER_MODEL is the recommended approach when referring to a user model in a models.py file.

What is AbstractUser in Django?

AbstractUser class inherits the User class and is used to add Additional Fields required for your User in Database itself. SO its change the schema of the database. It is basically used to add fields like date_of_birth , location and bio etc. to the existing User model This is Done to the very Beginning of the project.


2 Answers

Django provides built-in support for extending the user with your own properties, called User Profiles.

like image 178
DMac the Destroyer Avatar answered Sep 28 '22 00:09

DMac the Destroyer


That is generally the method I prefer, I don't like to touch the Django user just to be safe. This way you know you aren't going to conflict with anything in the user model, and it makes it clear which code is framework code and which is your code.

I normally don't call it MyAppUser but more usually, something like UserSettings. I don't want my user related classes to be confused as replacements for the User object, but simply provide more information.

Generally in apps you will have foreign keys to User all over the place, so I still use Django User class for all those as well. I find it keep things cleaner.

I have tried to subclass User and found it didn't give me much, and ended up confusing things, more than they needed to be.

like image 24
Kekoa Avatar answered Sep 28 '22 00:09

Kekoa