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?
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.
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.
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.
Django provides built-in support for extending the user with your own properties, called User Profiles.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With