您好,登錄后才能下訂單哦!
自定義列表
對象顯示
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True, verbose_name='e-mail') def __unicode__(self): # 顯示的方式 return u'%s %s' % (self.first_name, self.last_name)
作者列表
from django.contrib import admin from mysite.books.models import Publisher, Author, Book class AuthorAdmin(admin.ModelAdmin): # 用AuthorAdmin選項注冊Author模塊 list_display = ('first_name', 'last_name', 'email') ############################ admin.site.register(Publisher) admin.site.register(Author, AuthorAdmin) # admin.site.register()函數接受一個ModelAdmin子類作為第二個參數。 admin.site.register(Book)
添加一個快速查詢欄
class AuthorAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'email') search_fields = ('first_name', 'last_name') # 過濾的依據 #############
為Book列表頁添加一些過濾器
from django.contrib import admin from mysite.books.models import Publisher, Author, Book class AuthorAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'email') search_fields = ('first_name', 'last_name') class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publication_date',) ######################### admin.site.register(Publisher) admin.site.register(Author, AuthorAdmin) admin.site.register(Book, BookAdmin)
一種過濾日期的方式是使用date_hierarchy選項
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publication_date',) date_hierarchy = 'publication_date' # 請注意,date_hierarchy接受的是* 字符串* ,而不是元組。
改變默認的排序方式
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering = ('-publication_date',) # 按publication date降序排列 小箭頭顯示排序
自定義編輯表單
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering = ('-publication_date',) fields = ('title', 'authors', 'publisher', 'publication_date') # fields 內表示可編輯的 我們可以隱藏publication_date,以防止它被編輯。如: fields = ('title', 'authors', 'publisher') # 這里的順序會影響顯示的順序
filter_horizontal
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publisher',) date_hierarchy = 'publication_date' ordering = ('-publication_date',) fields = ('authors', 'title', 'publisher', 'publication_date') ## 這個是順序,翻譯說要移除,原文明明是提醒讀者不要移除 filter_horizontal = ('authors',) ########################### publisher一定要在fields 中
選擇框
class BookAdmin(admin.ModelAdmin): ... raw_id_fields = ('publisher',) ####### 這條語句將默認是下拉框的變成文本框(旁邊的放大鏡可彈框選擇)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。