Using a OneToOneField relationship with the User model
One of the simplest ways to store custom user information in Django is by creating a new model and creating a OneToOneField
relation with the default User
model. Let us take a simple example to implement this approach.
We need to store the phone number and city for each user registering on our platform, but Django’s default User
model does not have these fields present. We should create a new model to store this information for each user.
First, create a new custom Django app, custom_user
. Then, add a new UserProfile
model in the custom_user/models.py
file:
from django.db import models class UserProfile(models.Model): user = models.OneToOneField('auth.User', related_name='user_profile', on_delete=models.CASCADE) phone_no = models.CharField(unique=True, max_length=20) city = models.CharField(max_length=40) ...