20

hello i'm new in python and django I need a view that get current user profile I know I shoud use get_profile from User but I don't know how to use it . i read the django document and It didn't help me. this is what I found from doc:

from django.contrib.auth.models import User
profile=request.user.get_profile()
nim4n
  • 1,813
  • 3
  • 21
  • 36

4 Answers4

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
qris
  • 7,900
  • 3
  • 44
  • 47
César
  • 9,939
  • 6
  • 53
  • 74
  • Tnx for your answer but my broblem is in the view part .. I already have my model and I don't know how to use my model detail in view – nim4n Nov 19 '12 at 20:51
  • @nim4n inside your views you already have the User inside the request. So, to get the profile you just need to do: profile = request.user.get_profile() – César Nov 19 '12 at 20:56
  • does it work on 1.5? It seems that since 1.5 there is no auth_profile, but auth_model – Павел Тявин Nov 19 '12 at 22:04
  • 5
    @ПавелТявин AUTH_PROFILE_MODULE is not supported in Django 1.5 – César Nov 19 '12 at 22:09
  • last night I tried so many time and I get some error. today I runserver to write the error here but It works fine!! I didn't change anything from last night :) – nim4n Nov 20 '12 at 06:57
  • 13
    AUTH_PROFILE_MODULE and get_profile() are deprecated. Please add that to your answer – Ajoy Nov 12 '14 at 05:01
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
  • 1,132
  • 2
  • 13
  • 31
  • Tnx for detail. I created my user profile and I use admin interface for manage that but I need to get current user profile in my view and I have no idea how to do It – nim4n Nov 19 '12 at 20:38
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
  • 1,468
  • 19
  • 36
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/

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • Could you please check this SO question http://stackoverflow.com/questions/17806683/create-row-of-date-while-creating-superuser and guide me how to implement this. – user2086641 Jul 23 '13 at 14:46
  • In the question you referenced, the User doesn't have a "location" attribute because Location has a ForeignKey to User. So, an instance of User would have a "location_set" attribute. – Brandon Taylor Jul 23 '13 at 15:12
  • I tried with OneToOneField,that time i got this issue"setting.models.DoesNotExist: Location matching query does not exist. " in console while creating superuser. – user2086641 Jul 23 '13 at 15:15
  • Hard to say without seeing some code :) Post a question with some sample code so I and others can get a better sense of what's going on. – Brandon Taylor Jul 23 '13 at 15:30
  • I updated the tried code and traceback in the question,please check. – user2086641 Jul 23 '13 at 16:01
  • An instance of User will not have a location property that points to a Location model. It will have a `location_set` property, which contains one or more Location instances. – Brandon Taylor Jul 23 '13 at 16:58