Django:將模型注冊到后台的幾種方法


from django.contrib import admin
from .models import *

#將模型注冊到后台:
#方法一:將模型直接注冊到后台
# admin.site.register(Product)

#方法二:自定義ProductAdmin類,並繼承ModelAdmin
#使用python裝飾器將ProductAdmin和模型Product綁定並注冊到后台
# @admin.register(Product)
# class ProductAdmin(admin.ModelAdmin):
#     list_display = ['id', 'name', 'weight', 'size', 'type']

#方法三:
class ProductAdmin(admin.ModelAdmin):
    # pass
    list_display = ['id', 'name', 'weight', 'size', 'type']
    search_fields = ['id', 'name', 'type__type_name']
    list_filter = ['type__type_name', 'name']
    ordering = ['-id']
    # date_hierarchy = Field
    fields = ['name', 'weight', 'size', 'type']
    readonly_fields = ['name']
admin.site.register(Product, ProductAdmin)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM