python之Django admin總結,pythondjangoadmin

來源:互聯網
上載者:User

python之Django admin總結,pythondjangoadmin

一.Django內建admi

     a.配置路由

urlpatterns = [        url(r'^admin/', admin.site.urls),    ]

    b.定製admin 在admin.py中只需要講Mode中的某個類註冊,即可在Admin中實現增刪改查的功能,如:

admin.site.register(models.UserInfo)
但是,這種方式比較簡單,如果想要進行更多的定製操作,需要利用ModelAdmin進行操作,如:
方式一:    class UserAdmin(admin.ModelAdmin):        list_display = ('user', 'pwd',)     admin.site.register(models.UserInfo, UserAdmin) # 第一個參數可以是列表
方式二:    @admin.register(models.UserInfo)                # 第一個參數可以是列表    class UserAdmin(admin.ModelAdmin):        list_display = ('user', 'pwd',)

3.ModelAdmin中提供了大量的可定製功能,如:

1. list_display,列表時,定製顯示的列。

class UserAdmin(admin.ModelAdmin):    list_display = ('user', 'pwd', 'xxxxx')     def xxxxx(self, obj):        return "xxxxx"

 

2. list_display_links,列表時,定製列可以點擊跳轉。

 

class UserAdmin(admin.ModelAdmin):    list_display = ('user', 'pwd', 'xxxxx')    list_display_links = ('pwd',) 

3. list_filter,列表時,定製右側快速篩選。

 

class UserAdmin(admin.ModelAdmin):     list_display = ('user', 'pwd')    class Ugg(admin.SimpleListFilter):        title = _('decade born')        parameter_name = 'xxxxxx'list_filter = ('user',Ugg,)

 

4. list_select_related,列表時,連表查詢是否自動select_related

5. 分頁相關

 

# 分頁,每頁顯示條數    list_per_page = 100 # 分頁,顯示全部(真實資料<該值時,才會有顯示全部)    list_max_show_all = 200 # 分頁外掛程式    paginator = Paginator

 

6. list_editable,列表時,可以編輯的列

 

class UserAdmin(admin.ModelAdmin):    list_display = ('user', 'pwd','ug',)    list_editable = ('ug',)

 

7. search_fields,列表時,模糊搜尋的功能

class UserAdmin(admin.ModelAdmin):         search_fields = ('user', 'pwd'

8. date_hierarchy,列表時,對Date和DateTime類型進行搜尋

 

class UserAdmin(admin.ModelAdmin):     date_hierarchy = 'ctime'

 

9. preserve_filters,詳細頁面,刪除、修改,更新後跳回列表後,是否保留原搜尋條件

10. save_as = False,詳細頁面,按鈕為“Sava as new” 或 “Sava and add another”

11. save_as_continue = True,點擊儲存並繼續編輯

 

save_as_continue = True # 如果 save_as=True,save_as_continue = True, 點擊Sava as new 按鈕後繼續編輯。# 如果 save_as=True,save_as_continue = False,點擊Sava as new 按鈕後返回列表。

 

12. save_on_top = False,詳細頁面,在頁面上方是否也顯示儲存刪除等按鈕

13. inlines,詳細頁面,如果有其他表和當前表做FK,那麼詳細頁面可以進行動態增加和刪除

 

class UserInfoInline(admin.StackedInline): # TabularInline    extra = 0    model = models.UserInfo  class GroupAdminMode(admin.ModelAdmin):    list_display = ('id', 'title',)    inlines = [UserInfoInline, ]

 

14. action,列表時,定製action中的操作

 

class UserAdmin(admin.ModelAdmin):     # 定製Action行為具體方法    def func(self, request, queryset):        print(self, request, queryset)        print(request.POST.getlist('_selected_action'))     func.short_description = "中文顯示自訂Actions"    actions = [func, ]     # Action選項都是在頁面上方顯示    actions_on_top = True    # Action選項都是在頁面下方顯示    actions_on_bottom = False     # 是否顯示選擇個數    actions_selection_counter = True

 

15. 定製HTML模板

 

add_form_template = Nonechange_form_template = Nonechange_list_template = Nonedelete_confirmation_template = Nonedelete_selected_confirmation_template = Noneobject_history_template = None

 

16. raw_id_fields,詳細頁面,針對FK和M2M欄位變成以Input框形式

class UserAdmin(admin.ModelAdmin):     raw_id_fields = ('FK欄位', 'M2M欄位',)

17. fields,詳細頁面時,顯示欄位的欄位

 

class UserAdmin(admin.ModelAdmin):    fields = ('user',)

 

18. exclude,詳細頁面時,排除的欄位

 

class UserAdmin(admin.ModelAdmin):    exclude = ('user',)

 

19. readonly_fields,詳細頁面時,唯讀欄位

 

class UserAdmin(admin.ModelAdmin):    readonly_fields = ('user',

 

20. fieldsets,詳細頁面時,使用fieldsets標籤對資料進行分割顯示

 

class UserAdmin(admin.ModelAdmin):    fieldsets = (        ('基本資料', {            'fields': ('user', 'pwd', 'ctime',)        }),        ('其他', {            'classes': ('collapse', 'wide', 'extrapretty'),  # 'collapse','wide', 'extrapretty'            'fields': ('user', 'pwd'),        }),    )

 

21. 詳細頁面時,M2M顯示時,資料移動選擇(方向:上下和左右)

 

class UserAdmin(admin.ModelAdmin):    filter_vertical = ("m2m欄位",) # 或filter_horizontal = ("m2m欄位",)

22. ordering,列表時,資料定序

 

class UserAdmin(admin.ModelAdmin):    ordering = ('-id',)    或    def get_ordering(self, request):        return ['-id', ]

 

23. view_on_site,編輯時,是否在頁面上顯示view on set

 

view_on_site = False或def view_on_site(self, obj):    return 'https://www.baidu.com'

 

24. radio_fields,詳細頁面時,使用radio顯示選項(FK預設使用select)

radio_fields = {"ug": admin.VERTICAL} # 或admin.HORIZONTAL

25. show_full_result_count = True,列表時,模糊搜尋後面顯示的資料個數樣式

 

class UserAdmin(admin.ModelAdmin):    # show_full_result_count = True # 1 result (12 total)    # show_full_result_count = False  # 1 result (Show all)    search_fields = ('user',)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.