How to extend User Model in Django

In the previous post, I discussed how you can create a custom User model in Django but what if you don’t want to create a custom one.

In that case, You can just extend the User model with a OneToOne model relationship.

We can easily add another fiel…


This content originally appeared on DEV Community and was authored by Kritebh Lagan Bibhakar

In the previous post, I discussed how you can create a custom User model in Django but what if you don't want to create a custom one.

In that case, You can just extend the User model with a OneToOne model relationship.

  • We can easily add another field such as Location, Date of Birth etc.

Create a Django Project

Setup your Virtual environment and create a Django Project.

Note - Set up code is available at previous post.

Extending User Model

Import the User model from "django.contrib.auth.models" and we will create another model which will relate to this User model

from django.db import models
from django.contrib.auth.models import User
from django.db.models.base import Model
from django.db.models.deletion import CASCADE
# Create your models here.
class ExtendUser(models.Model):
    r = models.OneToOneField(User,on_delete=models.CASCADE)
    date_of_birth = models.DateField(null=True)
    city = models.CharField(max_length=30)
    def __str__(self):
        return self.r.username

Add this model to the admin.py

Showing Data

Now the question is how we can access this data and show it at the frontend.

It's easy..

In views.py file I have created a single function which will send the data as context.

def home(request):
    data = request.user
    return render(request,'core/index.html',{'data':data})

Now in index.html you can access this data

<p>{{data.username}}</p>
<p>{{data.extenduser.city}}</p>
<p>{{data.extenduser.date_of_birth}}</p>

Cons

  • By this method, you can't change the primary login method which is username.

Here is the GitHub repo for the code

GitHub logo kritebh / extend-user-model-django

How to extend User model in Django


This content originally appeared on DEV Community and was authored by Kritebh Lagan Bibhakar


Print Share Comment Cite Upload Translate Updates
APA

Kritebh Lagan Bibhakar | Sciencx (2021-05-29T06:46:18+00:00) How to extend User Model in Django. Retrieved from https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/

MLA
" » How to extend User Model in Django." Kritebh Lagan Bibhakar | Sciencx - Saturday May 29, 2021, https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/
HARVARD
Kritebh Lagan Bibhakar | Sciencx Saturday May 29, 2021 » How to extend User Model in Django., viewed ,<https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/>
VANCOUVER
Kritebh Lagan Bibhakar | Sciencx - » How to extend User Model in Django. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/
CHICAGO
" » How to extend User Model in Django." Kritebh Lagan Bibhakar | Sciencx - Accessed . https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/
IEEE
" » How to extend User Model in Django." Kritebh Lagan Bibhakar | Sciencx [Online]. Available: https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/. [Accessed: ]
rf:citation
» How to extend User Model in Django | Kritebh Lagan Bibhakar | Sciencx | https://www.scien.cx/2021/05/29/how-to-extend-user-model-in-django/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.