How to Extend Django User Model in a Custom User Model
Posted By : Amit Patel | 29-Nov-2018
Quick Summary:-
In this blog, we will learn how to create a new Django project with a custom User model. This means how to extend Django User model fields in a custom User model.
Prerequisites:-
- I have used python version 3.5.2
- Django version 2.0.2
- Operating System Ubuntu 16.04
- mysql
- IDE - pycharm-community-2018.2.5
Let's start to create Django project:-
# This command will create Django project with demopythonproject name. $ django-admin.py startproject demopythonproject # View the list of files or folders. $ ls # Select dictory/folder. $ cd demopythonproject
Now, create users app:-
# In Django, we create app module "users" using this command. $ python3 manage.py startapp users
And run the project-
# Using this command, we will start the project. $ python3 manage.py runserver
If we navigate this Url http://127.0.0.1:8000. We'll see Django welcome page.
Custom User Model:-
We will add "users" app in settings.py and use the "AUTH_USER_MODEL" config to tell Django to use our new custom user model in place of Django User model. And our custom user model will CustomUser.
Add "users.apps.UsersConfig" at the bottom of "INSTALLED_APPS". And add the "AUTH_USER_MODEL" config at the bottom of the entire file.
## demopythonproject/settings.py INSTALLED_APPS = [ ...... ...... ...... ...... 'users.apps.UsersConfig', ] ...... ...... ...... AUTH_USER_MODEL = 'users.CustomUser'
Update database configuration in settings.py-
## demopythonproject/settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'custom', 'USER': 'root', 'PASSWORD': 'root', 'HOST': 'localhost', 'PORT': '3306', } }
where 'custom' is database name. And we will create 'custom' database through terminal.
In modles.py, we will add CustomUser model.
CustomUser model, that means it is a entity class. And created "users_customuser" table in "custom" database. Where all entry of CustomUser will be store.
## users/modles.py from django.contrib.auth.models import AbstractUser from django.db import models # Create your models here. class CustomUser(AbstractUser): # add additional fields in here description = models.CharField(max_length=100, default='') city = models.CharField(max_length=100, default='') website = models.URLField(default='') phoneNumber = models.IntegerField(default=0) image = models.ImageField(upload_to='profile_image', blank=True) def __str__(self): return self.email
Now we can run makemigrations and migrate commands to create a new database that uses the custom user model.
$ python3 manage.py makemigrations users $ python3 manage.py migrate
Conclusion:-
In this article, we have covered how to create a custom User model to extend Django User model fields.
Reference:-
External links to the source code-
1)- https://wsvincent.com/django-custom-user-model-tutorial
2)- https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Patel
Amit Patel is having good knowledge of java,have expertise in hibernate.