by ahin subhra das
Django is one of the most popular framework in python . Django Admin is one of the most important tools of Django. It’s a full-fledged application with all the utilities a developer need. Django Admin’s task is to provide an interface to the admin of the web project.
Firstly we need to create a new project in django with the command :
django-admin startproject cust_admin [project name :cust_admin]
Under this project we need to create a new app for better experience . for that we use: python manage.py startapp app_cust_admin
[app name:app_cust_admin]
After that we must create a new superuser for our project before that if we run migrations then it is helpful for our project .
For migrations :
- python manage.py makemigrations
- python manage.py migrate
For run the server : python manage.py runserver
To Create super user :
python manage.py createsuperuser
After creating a new superuser anyone easily access admin tool of that project from http://127.0.0.1:8000/admin/ this url .
For more clear knowledge we need to create a model of any datasets [like user details or any product or student details any].
In app_cust_admin we have a file named models.py , we need to add code for our model.
models.py
from django.db import models
# Create your models here.
class User_list(models.Model) :
fullname = models.CharField(max_length=200,null=True)
app_id = models.IntegerField(null=True)
profession = models.CharField(max_length=200,null=True)
email = models.EmailField(max_length = 254,null=True)
status = models.CharField(max_length=50,default=’1')
class Meta:
app_label = ‘app_cust_admin’
In admin.py we need to add:
from django.contrib import admin
from .models import User_list
# Register your models here.
admin.site.register(User_list)
admin.site.site_header = “Django admin Customization”
After that we need to run migrations for another time:
Now we want to show data of user_list in our admin dashboard, for that we add code in admin.py:
from django.contrib import admin
from .models import User_list
# Register your models here.
class HomeAdmin(admin.ModelAdmin):
list_display = (‘fullname’, ‘app_id’,’profession’,’email’,’status’)
admin.site.register(User_list,HomeAdmin)
After that we add filter in our admin to get more filterd data , here we use profession as filter parameter:
admin.py
after list_display ->
#filter your data as you want
list_filter = [‘profession’]
After adding filter we are moving to django admin actions. This is a very important tool in admin with this you can do many types of operations . For example here we try to do a duplicate data generating action button.
Firstly we create a new file named actions.py in actions.py we write this code :
import csv
from django.http import HttpResponse
from datetime import datetime
from .models import User_list
from django.contrib import messages
def create_duplicate_action():
def create_duplicate(modeladmin, request, queryset):
if len(queryset) > 1:
messages.error(request , “Can not select more than one lead.”)
else:
obj = queryset.values(‘fullname’, ‘app_id’,’profession’,’email’,’status’)[0]
obj[‘status’] = 1
User_list.objects.create(**obj)
messages.info(request, “Successfully created duplicate lead.”)
return True
create_duplicate.short_description = “Create Duplicate User”
return create_duplicate
Now we add some line of code in admin.py :
for link actions.py we add : from .actions import create_duplicate_action
and
after list_filter we add : actions = [create_duplicate_action()]
With this operation we easily make a duplicate data of present record .
This type of many operations we can implement in our django admin .
I just give here a small outfit of django admin customization. Plese give your feedback about my article under the comment section.