What is Django Registration?
Django Registration is the process of user registration in Django’s authenticate and authorized admin panel. Till now, we are using python manage.py createsuperuser command to get login credentials. But now, we will discuss about how to get registered on that system. Furthermore, we will discuss about login system.
This tutorial basically teaches you how to register on Djnago’s authentication and authorization system. Basically it avoids to create superuser using command prompt, i.e. using GUI(templates) instead.
To register on Django site, first of all you need to understand about User Model. Now we will discuss about how to use Django’s built in tools to create user authorization models.Here are some important points:
- You can see in the admin panel, there are “users”, Let’s learn how to use this feature.
- There were two things(users and groups) in admin, we will use “users”.
- The user object has a few key features:
- First Name
- Last Name
- There are also some other attributes for the user object, such as:
- Date joined etc
- Moreover, You can also add more attributes to user such as images, links etc.
- You can create User Model in your application’s models.py file by creating another class.
- This class has a relationship to the built in User class.
Now we will create a model class using built in User model.Here is my project’s directory:
Step 1: Create model using built in User model
- Import User model from djnago > contrib > auth directrory
firstproject > myapp > models.py
from django.db import models from django.contrib.auth.models import User class registerModel(models.Model): user = models.OneToOneField(User, on_delete = models.CASCADE) def __str__(self): return self.user.username
What is OneToOneField ?
Here, One to one field means that, child table registerationModel has an one to one relationship with parent model User. One to one relationship means username field of User(parent) table relates to one and only one time to username field of child table.
- Add model to admin.py file
firstproject > myapp > admin.py
from django.contrib import admin from .models import registerModel admin.site.register(registerModel)
- Apply Migrations using:
- python manage.py makemigrations
- python manage.py migrate
- And check the output in admin model.
-You can see below the model we created:
-You can see below as there is only single superuser in admin i.e. showing in dropdown menu:
Download source code
So it is all about creating model from built in User Model. In our further tutorials we will discuss about creating model forms and complete django registration. I hope you guys enjoyed the post. Thanks!!!