Admin Django amdin is a django admin page that provides complete HTML and CSS for admin pages. It allows you to add, delete, modify, and query data from a database after creating a table using the django Model. Present in the page for management. By default, it’s pretty powerful, and if you don’t need complicated functionality, it’s fine, but sometimes you need to customize specific features, such as the search function. This series of articles is a step-by-step guide to customizing your Own Admin application.

1 First let’s create a Django project with PyCharm

2 Create a database table (models.py under APP01)

from django.db import models

# Create your models here.
class Publisher(models.Model):
   name = models.CharField(max_length=30, verbose_name="Name")
   address = models.CharField("Address", max_length=50)
   city = models.CharField('city'. max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField() class Meta: verbose_name ='Publisher'
       verbose_name_plural = verbose_name

   def __str__(self):
       return self.name

class Author(models.Model):
   name = models.CharField(max_length=30)

   def __str__(self):
       return self.name

class AuthorDetail(models.Model):
   sex = models.BooleanField(max_length=1, choices=((0, 'male'), (1, 'woman'),))
   email = models.EmailField()
   address = models.CharField(max_length=50)
   birthday = models.DateField()
   author = models.OneToOneField(Author, on_delete=None)

class Book(models.Model):
   title = models.CharField(max_length=100)
   authors = models.ManyToManyField(Author)
   publisher = models.ForeignKey(Publisher, on_delete=None)
   publication_date = models.DateField()
   price = models.DecimalField(max_digits=5, decimal_places=2, default=10)
   def __str__(self):
       return self.title
Copy the code

3 Register and configure the Django admin background page

There are two ways to register the medel class with admin:

Admin.site. register(Book,MyAdmin) <2> Use register decorator @admin.register(Book)Copy the code

Know the ModelAdmin

A custom class for managing interfaces that you inherit from if you want to extend a specific Model interface.

App01 under admin. Py

from django.contrib import admin
# Register your models here.
from app01.models import *
# Register your models here.
# @admin.register(Book)#-----> Add a custom to a single table
class MyAdmin(admin.ModelAdmin):
   list_display = ("title"."price"."publisher")
   search_fields = ("title"."publisher__name")
   list_filter = ("publisher__name",)
   ordering = ("price",)
   fieldsets =[
       (None,               {'fields': ['title']}),
       ('price information', {'fields': ['price'."publisher"."publication_date"].'classes': ['collapse']}), ] admin.site.register(Book,MyAdmin) admin.site.register(Publisher) admin.site.register(Author) Admin.site. register(AuthorDetail) list_display: specifies the field to display search_fields: specifies the field to search for list_filter: Specify a list filter without ordering. Specify a list filter without orderingCopy the code

4 Open Terminal, synchronize databases, and create an administrator user

Synchronizing databases

python manage.py makemigrations

python manage.py migrate

Create an administrator user

python manage.py createsuperuser

5 The default management page is In English. You can modify the following options in the setting.py file to change the Settings to Chinese

LANGUAGE_CODE = ‘zh-hans’

6 to start the project, visit http://127.0.0.1:8000/admin, interface is as follows

7 Enter the administrator username and password

After 8, you can add, delete, change and check the data, ok? Isn’t it powerful!