Extending User Model in Django

Posted on by Francis David Cleary

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).

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.1

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:

You can read more about this in the django docs for storing additional information about users.  
 
 
 
if created is not really needed but is a good double check along with UserProfile.objects.get_or_create. Using this we should never have an error of any user duplications or any other weird bugs/quarks your code may produce.

 

This entry was posted in development, tutorials and tagged , . Bookmark the permalink.
  • Anonymous

    OneToOneField relations are already available on User model: just use my_user.userprofile in your case (or rename the model to Profile and use my_user.profile). You don’t need the AUTH_PROFILE_MODULE and get_profile() in most cases.
    Another trick is to use AutoOneToOneField (see https://bitbucket.org/wrar/django-annoying/src/7d7237a708ab/annoying/fields.py ): this way profile will be auto created on first access and signals are not needed.

  • http://profiles.google.com/mjs7231 Michael Shepanski

    You can remove line #17 and things will work the same (what kmike said).  No monkey patching is a good thing. ;)

  • Pingback: Extending User Model in Django | Tivix Blog | Programmer Solution

  • sdds

    ed

  • Aswani Kumar

    how to add user to model, like author=user who created the record.

Tivix

A full service boutique design and technology firm specializing in developing dynamic solutions which help organizations to engage consumers, and build relationships in unique, powerful new ways.

What We Do... why we're different!

We build high-engagement web, mobile, and social apps. We build them for startups, established brands, nonprofits, and organizations of all kinds. The Tivix team brings a unique combination of savvy designers, rock-solid engineers, and deep business experience.

Facebook