How to Add Model to Django Admin Site [Step-by-step]
Do you want to see all the data submitted by the user on your Django website? That’s exactly you need an admin dashboard where you can see all the models and data entries.
Django has a special admin dashboard feature. In Django, models are defined to store the user data.
As part of the Django tutorial, we are going to see how we can add model to Django admin so that we can see all the model data entries from users.
Step 1: Create model in Django
If you have already created model, you can skip this step.
As an example, let’s take a model called
ContactModel which has three fields-
nameto store the name of the user
mobileto store the mobile number of the user
You can define the model in the
models.py as below.
class ContactModel(models.Model): name = models.CharField(max_length=120) mobile = models.IntegerField() email = models.EmailField()
In an earlier tutorial, we have learned about creating a form in Django. Similarly you can create the contact form to allow users to enter the data that will be saved in this model.
Being admin, we might want to see who all have contacted us by submitting the custom contact form.
Django has admin dashboard feature by which we can see or track all the data models or information.
Once user share the contact detail by filling the contact form, let’s see how we can check the information.
Step 2: Add model to Django admin
You can check the
admin.py file in your project app directory. If it is not there, just create one.
admin.py and add below lines of code to register model for admin dashboard.
from django.contrib import admin from .models import ContactModel @admin.register(ContactModel) class RequestDemoAdmin(admin.ModelAdmin): list_display = [field.name for field in ContactModel._meta.get_fields()]
Here, we are showing all the model fields in the admin site. You can also customize the
list_display list by specifying selected fields.
For example, we want to display only a name and an email ID (and not a mobile number) in the Django admin site.
from django.contrib import admin from .models import ContactModel @admin.register(ContactModel) class RequestDemoAdmin(admin.ModelAdmin): list_display = ['name', 'email']
That’s all. Now run your Django project.
Step 3: Execute and run project
If you have created the new model, you have to migrate the models into the database. Execute the below command in the console using
python manage.py makemigrations
python manage.py migrate
Now let’s start your Django project by running Django server.
python manage.py runserver
Once it is started, open the Django admin dashboard in the browser.
Once you login into your admin dashboard, you can see the contact model in your dashboard.
Click on the “Contact model”. You will see all the model data entries.
ID is autogenerated fields that uniquely define each entry in the model.
This is all about how you can add model to Django admin. If you find any difficulty, let me know in the comment section below.