Django-7-admin

  1. 激活管理界面,创建用户:python manage.py createsuperuser

  2. 管理某个model对象

     # /user/admin.py
     from .models import UserInfo
     admin.site.register(UserInfo)
    
  3. 利用ModelAdmin定制

     # /user/admin.py
     @admin.register(User)
     class UserAdmin(admin.ModelAdmin):
         fields = ['telephone']
     # 或者
     class UserAdmin(admin.ModelAdmin):
         fields = ['telephone']
     
     admin.site.register(User,UserAdmin)
    
     # 定制
     @admin.register(User)
     class UserAdmin(admin.ModelAdmin):
         fields = ['telephone','password','email']  # 详情显示的内容
         list_display = ('telephone','password','pub_time')  # 定制显示的列
         list_display_links = ('telephone',)  # 连接
         list_filter = ('telephone',)  # 筛选
         search_fields = ('telephone',) # 搜索
         date_hierarchy = ('pub_time') # 根据日期展示
    
  4. 添加关联对象

    一对多:在主表添加从表的Inline

     class UserHobbyInline(admin.StackedInline):
         model = UserHobby
         extra = 0
     class InfoInline(admin.StackedInline):
         model = UserInfo
         extra = 0
     
     # 利用 ModelAdmin 定制
     @admin.register(User)
     class UserAdmin(admin.ModelAdmin):
         fields = ['telephone','password','email']  # 详情显示的内容
         list_display = ('telephone','password','pub_time')  # 定制显示的列
         list_display_links = ('telephone',)  # 连接
         list_filter = ('telephone',)  # 筛选
         search_fields = ('telephone',) # 搜索
         date_hierarchy = ('pub_time') # 根据日期展示
     
         inlines = [InfoInline,UserHobbyInline]
    

    多对多:list_display可以增加函数实现,但是fields目前没找到方法QWQ。。。

你可能感兴趣的:(Django)