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)
...
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?
- [Django]-Django: Get model from string?
- [Django]-Django, Models & Forms: replace "This field is required" message
18
Current Django is 1.9 and here are some updates to the outdated accepted answer
- use
models.OneToOneField(User)
- add
related_name='profile'
- 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.
- [Django]-What is the best way to upload files in a modern browser
- [Django]-Django: How to set a field to NULL?
- [Django]-Resource temporarily unavailable using uwsgi + nginx
16
Django provides a way of storing additional information about users in a separate table (called user profile).
- [Django]-Django โ accessing the RequestContext from within a custom filter
- [Django]-Why use Django on Google App Engine?
- [Django]-Django: How do I add arbitrary html attributes to input fields on a form?
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.
- [Django]-How can I get access to a Django Model field verbose name dynamically?
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
- [Django]-"gettext()" vs "gettext_lazy()" in Django
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
.
- [Django]-Atomic increment of a counter in django
- [Django]-How to redirect to previous page in Django after POST request
- [Django]-Django query annotation with boolean field
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.
- [Django]-How to escape {{ or }} in django template?
- [Django]-Django REST Framework โ Serializing optional fields
- [Django]-ForeignKey to abstract class (generic relations)
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 theAUTH_USER_MODEL
setting has been changed to a different user model. [..] Instead of referring toUser
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
- [Django]-Running "unique" tasks with celery
- [Django]-Database returned an invalid value in QuerySet.dates()
- [Django]-How to set another Inline title in Django Admin?
2
If you want to get user profile data from user objects.
from django.contrib.auth.models import User
request.user.profile
- [Django]-How can I get all the request headers in Django?
- [Django]-Django required field in model form
- [Django]-How to perform filtering with a Django JSONField?