SoFunction
Updated on 2024-11-12

Django backend admin use details

Short Description.

Django's admin can provide a powerful background management functions, you can operate the database in the web interface, we need to modify the data table to be operated to register to the background management

Create a data table:

For demonstration purposes, let's create an img data table rule in the

The verbo_name in the figure is the name of the table field to be displayed in the admin interface, and the verbo_name in the defined class Meta is the name of the table to be displayed in the admin interface

modify a document

from  import admin
from app1 import models
# Register your models here.

()  #Load the table in theadmininitial recognition

Start the service and enter the interface

python  makemigrations # Generate create datasheet py file
python  migrate # Execute py files, update database
python  runserver #Starting services

Open your browser and visit http://127.0.0.1 :8000/admin

Enter the set account password to enter the interface

The default function can perform simple additions, deletions, and deletions to the table, and if you need to batch update or other operations, you need to customize the corresponding actions.

Customize the content to show.

When we choose to enter the interface of the table we created, the default content displayed is object

You can define the __str__ field by writing to the appropriate table in the

The effect is as follows

The default display is only one, we can modify it by defining list_display in ModelAdmin:

from  import admin
from  import img
# Register your models here.

class imgAdmin():
  list_display = ('title','summary','file')
  

(img,imgAdmin)

Clicking on a piece of data will show by default that each field is not an AutoField and that editable=True has the same order in the individual field set as the fields defined in the model.

We can define fields or exclude in ModelAdmin to modify it:

class imgAdmin():
  list_display = ('title','summary','file')
  fields = ('title',)

exclude is the opposite of fields.

Customized Actions

The default action is to delete only one

We can customize it through ModelAdmin

def change_up(modeladmin, request, queryset): #Define the action
  (stat='1')
change_up.short_description = "up" #Rewrite Action Display Name

def change_down(modeladmin, request, queryset):
  (stat='0')
change_down.short_description = "down"



class imgAdmin():
  list_display = ('title','summary','file','stat')
  fields = ('title',)
  actions = [change_up,change_down] #Bind Action

This is the whole content of this article.