Extending the User Model in Django
When building nearly every application in Django there comes a time where you want to associate more information with a user then just the built in ones. Django lets you as the programmer control how and where you want to handle this information.
There are several ways to do this, and there is no absolute right or wrong way. This is just one way to manage extra user data and we find it very easy to use and manage.
First we create a user profile model (Like to create a separate "accounts" app to house this data).
from django.contrib.auth.models import User from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
#other fields here
def __str__(self):
return "%s's profile" % self.user
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
User.profile = property(lambda u: u.get_profile() )
As you can see above is a normal model with a foreign key to the Django User model. Then add any other fields you would want to store for a user: date of birth, website, favorate color, or whatever. Then we are going to tie a signal to the creation of users. In this case we use the django post_save signal and set the sender as the User model. When a user is saved it will then call create_user_profile
.
On line 17
we are monkey patching the User object to have a new property. All this does is create a shortcut for our own use later. So rather then typing my_user.get_profile().fav_color
we can simply type my_user.profile.fav_color
Under the hood they are the same but I find one is more aesthetically pleasing then the other.
Lastly we need to tell django that we have a user profile so that my_user.get_profile()
works. In your settings.py simply add:
AUTH_PROFILE_MODULE = 'YOURAPP.UserProfile'
You can read more about this in the django docs for storing additional information about users.