Django-08-admin

1 Introduction

django admin is one of powerful features, it can read data from the database were presented in a page management. By default, its functionality has been very strong, if you do not need complex functions, it has been good enough, but sometimes, some special features need to customize, such as search, following the gradual deepening of this series of articles on how to admin to customize their own applications.

If you feel good with English interface, you can modify the following options setting.py file

LANGUAGE_CODE = 'en-us'  #LANGUAGE_CODE = 'zh-hans'

2.  Registration medel class in two ways to the admin:

<1> Method, use the register

class BookAdmin(admin.ModelAdmin):
    ...

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

<2> use the register decorator

@admin.register(Book)
class
...

3. Custom admin

Custom style # admin 
class BookAdmin (admin.ModelAdmin): 
    list_display = ( 'ID', 'name', '. Price', 'pubDate') # contents displayed 
    list_editable = ( 'name', ' price') # Editable content 
    filter_horizontal = ( 'authors',) # horizontal search box 
    list_per_page = 5 # per page the number of content 
    search_fields = ( 'id', ' name', 'publish__name') # Search keyword 
    list_filter = ( 'pubdate', 'publish') # ... by filtration, no need to write publish__name 
    ordering = ( '-Price', 'ID') # sorted by price, - for descending 
    fieldsets = [ 
        (None, { 'Fields': [ 'name' ]}), 
        ( 'Information. price', { 'Fields': ['. price ',' publish '],' classes': [ 'collapse']})
    ]

 

Guess you like

Origin www.cnblogs.com/lsf123456/p/11423192.html