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
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.
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Jquery template tags conflict with Django template!
- [Django]-Django models: mutual references between two classes and impossibility to use forward declaration in python
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.
- [Django]-Django – How to use decorator in class-based view methods?
- [Django]-Table thumbnail_kvstore doesn't exist
- [Django]-How to implement FirebaseDB with a Django Web Application
2đź‘Ť
You need to specify which class is your “Profile” by setting AUTH_PROFILE_MODULE = 'accounts.UserProfile'
(for example)
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
- [Django]-How to test auto_now_add in django
- [Django]-Django annotation with nested filter