[Django]-Django user profile

48๐Ÿ‘

โœ…

You have to make a model for the user profile:

class UserProfile(models.Model):  
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=140)  
    gender = models.CharField(max_length=140)  
    employer = models.ForeignKey(Employer)
    profile_picture = models.ImageField(upload_to='thumbpath', blank=True)

    def __unicode__(self):
        return u'Profile of user: %s' % self.user.username

Then configure in settings.py:

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

39๐Ÿ‘

Conceptually, OneToOneField is similar to a ForeignKey with unique=True, but the โ€œreverseโ€ side of the relation will directly return a single object. This is the recommended way of extending User class.

class UserProfile(models.Model):  
    user = models.OneToOneField(User)
    ...

18๐Ÿ‘

Current Django is 1.9 and here are some updates to the outdated accepted answer

  1. use models.OneToOneField(User)
  2. add related_name='profile'
  3. use .__str__() and .format() for Python 3

like so

class UserProfile(models.Model):  
    user = models.OneToOneField(User, related_name='profile')
    location = models.CharField(max_length=140)  
    gender = models.CharField(max_length=140)  
    ...

    def __str__(self):
        return 'Profile of user: {}'.format(self.user.username)

Using related_name you can access a userโ€™s profile easily, for example for request.user

request.user.profile.location
request.user.profile.gender

No need for additional lookups.

16๐Ÿ‘

Django provides a way of storing additional information about users in a separate table (called user profile).

11๐Ÿ‘

Starting with Django 1.5 you can replace the default User with your custom user object using a simple settings entry:

AUTH_USER_MODEL = 'myapp.MyUser'

For slightly more details, check this Django documentation entry.

5๐Ÿ‘

Thereโ€™s a solution I found here. Basically you just extend the default form UserCreationForm but keeping the same name. It works seamlessly with the way Djangoโ€™s docs tell you to do UserProfiles.

2๐Ÿ‘

Answer can be updated to add signal receiver which will create the profile if it does not exist and update if it is already there.

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()

This https://simpleisbetterthancomplex.com/tutorial/2016/11/23/how-to-add-user-profile-to-django-admin.html post also includes how to edit, list the custom profile in admin panel.

2๐Ÿ‘

The current 2 top answers are outdated

If you reference User directly (for example, by referring to it in a foreign key), your code will not work in projects where the AUTH_USER_MODEL setting has been changed to a different user model. [..] Instead of referring to User directly [..] when you define a foreign key or many-to-many relations to the user model, you should specify the custom model using the AUTH_USER_MODEL setting.

from django.conf import settings
from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="userprofile",
    )

https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#referencing-the-user-model

2๐Ÿ‘

If you want to get user profile data from user objects.

from django.contrib.auth.models import User
request.user.profile

Leave a comment