Django's admin management tool

I. basic settings

1. Application registration

To display the app application in the background management, you need to register in admin.py. There are two ways to register

1.1 mode 1:

from django.contrib import admin
from blog.models import Blog
  
#Blog Manager of the model
class BlogAdmin(admin.ModelAdmin):
    list_display=('id', 'caption', 'author', 'publish_time')
     
#stay admin Register binding in
admin.site.register(Blog, BlogAdmin)

1.2 mode 2: using decorator

from django.contrib import admin
from blog.models import Blog
  
#Blog Manager of the model
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    list_display=('id', 'caption', 'author', 'publish_time')

2. Sinicization of admin interface

The default admin background management interface is English, which can be set in settings.py:

  LANGUAGE_CODE='zh-hans'

  TIME_ZONE='Asia/Shanghai'

II. Record list interface settings

1. Basic settings of record list

from django.contrib import admin
from app01.models import Book
from django.utils.safestring import mark_safe
#Blog Manager of the model @admin.register(Book) class BookAdmin(admin.ModelAdmin):
  def _



  def authors(self,obj):
    return ','.join([obj_author for obj_author in obj.author.all()])
#listdisplay Set the fields to display in the list( id Field is Django Default primary key of the model) list_display = ('name', 'publish', 'authors', 'pub_date','price') #list_per_page Set how many records are displayed per page. The default is 100 records list_per_page = 50 #ordering Set the default sorting field, negative sign means descending sorting ordering = ('-price',) #list_editable Set the default editable field list_editable = ['price'] #fk_fields Set display foreign key field fk_fields = ('machine_room_id',)
  #Set which fields can be clicked to enter the editing interface list_display_links = ('name', 'pub_date')
  #Sizer list_filter =('publish', 'author', ) #Filter search_fields =('name', 'net', 'mark') #Search field date_hierarchy = 'go_time' # Detailed time layered filtering 
  
  #Automatically query linked list select_related
  list_select_related

  #action,Customized action Operations in (batch operations)
  def func(self,request,queryset):
    queryset.update(price=100)
  func.short_description='Batch initialization'
  actions=[func,]

  #Customized HTML Template
  add_form_template=None
  change_form_template=None
  change_list_template=None
  delete_confirmation_template=None
  delete_selected_confirmation_template=None
  object_history_template=None

III. interpretation of admin source code

1. Start the admin.py file of all app s

def autodiscover():
    autodiscover_modules('admin', register_to=site)

2. Register model class

class BookAdmin(admin.ModelAdmin):      #Custom configuration class
    list_display = ("title",'publishDate', 'price')

admin.site.register(Book, BookAdmin) 
admin.site.register(Publish)

Execute the register method:

class ModelAdmin:        #Configuration class
  
pass
class
AdminSite: def __init__(self): self._registry = {} def register(self, model, admin_class=None, **options): admin_class = admin_class or ModelAdmin self._registry[model] = admin_class(model)
site=AdminSite()

After registering the model class, you just add a key value pair in the registry dictionary, take the model class as the key, and configure the class object as the value, as shown in the following figure:

Value obtained:
_registry={Book:BookAdmin(Book),Publish:ModelAdmin(Publish)}
So far, the registration process is completed

3. url configuration of admin

urlpatterns = [
    path('admin/', admin.site.urls),
]
class ModelAdmin():
    @property
    def urls(self):
        return self.get_urls(), None, None
     def get_urls(self):
        temp=[
            path('',self.list_view),
            path('add/',self.add_view),
            re_path('(\d+)/delete/',self.delete_view),
            re_path('(\d+)/edit/',self.edit_view),
        ]
        return temp
    
class AdminSite:
    @property
    def urls(self):
        return self.get_urls(), 'admin', self.name
    def get_urls(self):
        temp=[]
        for model,config_obj in self._registry.items():
            model_name = model._meta.model_name
            app_label = model._meta.app_label
            temp.append(path('%s/%s/'%(app_label,model_name),config_obj.urls))
        return temp

This completes the secondary distribution of the url

Tags: Python Django

Posted on Sun, 01 Dec 2019 02:28:59 -0800 by jodyanne