django Admin -admin

0922 self-summary

django Admin -admin

A. Model registration

admin.py

Registration way:

#在对于注册的app中的admin文件中导入模型然后注册模型
admin.site.register(导入的模型类)

Register Second way 该方法是Django1.7的版本新增的功能:

from django.contrib import admin
from blog.models import Blog
  
#Blog模型的管理器
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    list_display=('id', 'caption', 'author', 'publish_time')

Two .admin interface localization

默认admin后台管理界面是英文的,对英语盲来说用起来不方便。可以在settings.py中设置:
LANGUAGE_CODE = 'zh-CN'
TIME_ZONE = 'Asia/Shanghai'
1.8版本之后的language code设置不同:
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'

Three .admin interface optimization

1, record a list of basic settings

from django.contrib import admin
from blog.models import Blog
  
#Blog模型的管理器
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    #listdisplay设置要显示在列表中的字段(id字段是Django模型的默认主键)
    list_display = ('id', 'caption', 'author', 'publish_time')
    
    #list_per_page设置每页显示多少条记录,默认是100条
    list_per_page = 50
    
    #ordering设置默认排序字段,负号表示降序排序
    ordering = ('-publish_time',)
  
    #list_editable 设置默认可编辑字段
    list_editable = ['machine_room_id', 'temperature']
  
    #fk_fields 设置显示外键字段
     fk_fields = ('machine_room_id',)

2, Filter

from django.contrib import admin
from blog.models import Blog
  
#Blog模型的管理器
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    list_display = ('id', 'caption', 'author', 'publish_time')
     
    #筛选器
    list_filter =('trouble', 'go_time', 'act_man__user_name', 'machine_room_id__machine_room_name') #过滤器
    search_fields =('server', 'net', 'mark') #搜索字段
    date_hierarchy = 'go_time'    # 详细时间分层筛选 时间相关字段

Note here:

Use date_hierarchy detailed screening time when the error may appear: Database returned an invalid datetime value Are time zone definitions for your database and pytz installed.?

Approach:

Command line directly execute this command: [root @ mysql ~] # mysql_tzinfo_to_sql / usr / share / zoneinfo | mysql -u root mysql

Then restart the database can be.

Usually ManyToManyField many-field filter; title or the like with the search text box field; datetime layered screening.

If the filter is a foreign key to follow the syntax: This table fields Field __ foreign key table to be displayed. Such as: "user__user_name"

3, color display

from django.db import models
from django.contrib import admin
from django.utils.html import format_html
 
class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)
 
    def colored_name(self):
        return format_html(
            '<span style="color: #{};">{} {}</span>',
            self.color_code,
            self.first_name,
            self.last_name,
        )
 
class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name')

注意看上面代码,是写在models里,而不是admin中的ModelAdmin里

4. Adjust the head of the page displays the contents and page title

class MyAdminSite(admin.AdminSite):
    site_header = 'xx'  # 此处设置页面显示标题
    site_title = 'xxx'  # 此处设置页面头部标题
 
admin_site = MyAdminSite(name='management')

Note that: admin_site = MyAdminSite (name = 'management') brackets name value must be set here, otherwise it will not use the admin to set permissions, as to what the value is set, after I tested, had no effect.

See details: https: //www.cnblogs.com/wumingxiaoyao/p/6928297.html

Guess you like

Origin www.cnblogs.com/pythonywy/p/11568685.html