[Django]-Get user profile in django

46đź‘Ť

âś…

Django’s documentation says it all, specifically the part Storing additional information about users. First you need to define a model somewhere in your models.py with fields for the additional information of the user:

models.py

from django.contrib.auth.models import User

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)

    # Other fields here
    accepted_eula = models.BooleanField()
    favorite_animal = models.CharField(max_length=20, default="Dragons.")

Then, you need to indicate that this model (UserProfile) is the user profile by setting AUTH_PROFILE_MODULE inside your settings.py:

settings.py

...
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
...

You need to replace accounts with the name of your app. Finally, you want to create a profile every time a User instance is created by registering a post_save handler, this way every time you create a user Django will create his profile too:

models.py

from django.contrib.auth.models import User

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)

    # Other fields here
    accepted_eula = models.BooleanField()
    favorite_animal = models.CharField(max_length=20, default="Dragons.")


def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

Accessing the Profile

To access the current user’s profile in your view, just use the User instance provided by the request, and call get_profile on it:

def your_view(request):
    profile = request.user.get_profile()
    ...
    # Your code
👤César

6đź‘Ť

Basically django User models will provide access only for the fields ( firstname,lastname,email,password,is_staff,is_active,last_login).

However if we want to add any extra fields to this model, say we need to add a new column named dateofbirth for every user, then we need to add a column named DOB into User model. But this is not possible as we aren’t able to edit django User models.

To achieve this either

1.We can have a separate new table with email id & DOB column, such that a column in User model is mapped with a column in the new table. But this will create a new db instance for every db request. Say if u want to find the DOB of a customer,

  • First we need to fetch the value of mapped id of a customer from the
    User table.
  • WIth the above value, get DOB from the new table.

In the second method,

Instead of using django User model, use your own customize model with all the fields needed. However if any updation related to security or some enhancement made to django User model we can’t use it directly. We need to do more code changes at our end( wherever we use our customize models.) This will be a bit pain for a developer to identify the code & make changes.

To overcome the above issues, django introduce django profile which is very simple and more flexible. The advantages are

  • Updation/enhancement to the User model can be applied without modifying the code much
  • No need of creating new db instance to fetch the extra values.
  • Since the field has onetoone mapping deletion of data from one table will delete others also.
  • More secure, since we use django models ( no sql injection)

How to Use this:

In settings.py create a variable AUTH_PROFILE_MODULE = “appname.profiletable”

  • In models.py, create a new table with the fields needed and make sure that the id in User model is onetoone mapped with new table.
  • create a signal which inserts a row into the new table whenever a new entry is added into User model.
  • The value in the new table can be accessed using User object itself.

Say, we created a new table extrauser which has DOB, emailid. To find the DOB of a customer, use

a=User.objects.get(email='x@x.xom')
a.get_profile().DOB will give the dateofbirth value from extrauser table.

Hope the above details make you clear in understanding django profile. Incase of any help further, let me know. I have used django profile in my project.

👤Darknight

6đź‘Ť

Old question but I thought anyone seeing it today may benefit from this:

Django 1.5 adds the ability to – easily – extend the User model. This may be preferable as you now only got one object to deal with rather than two! Seems the more modern way.

https://hurricanelabs.com/blog/django-user-models/

👤wjdp

2đź‘Ť

You need to specify which class is your “Profile” by setting AUTH_PROFILE_MODULE = 'accounts.UserProfile' (for example)

https://docs.djangoproject.com/en/1.4/topics/auth/

Leave a comment