• Django之Admin


    二.定制Admin

    在admin.py中只需要讲Mode中的某个类注册,即可在Admin中实现增删改查的功能,如:

    admin.site.register(models.UserInfo)
    
    #admin.site.register(models.UserInfo,admin.ModelAdmin)  # 默认 admin.ModelAdmin
    

    这种方式比较简单,如果想要进行更多的定制操作,需要利用ModelAdmin进行操作,如:

    方式一:

    class UserAdimin(admin.ModelAdmin): def no_column(self,obj): return 'xxxxxx' list_display = ['name','email',no_column,'nickname'] admin.site.register(models.UserInfo,UserAdimin) 方式二: @admin.register(models.UserInfo) class UserAdimin(admin.ModelAdmin): def no_column(self,obj): return 'xxxxxx' list_display = ('name','email',no_column,'nickname') #list_display = (,)或[ ]

    1. list_display,列表时,定制显示的列。

    2.list_display_links,列表时,定制列可以点击跳转。

    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email',no_column)
    
        list_display_links = ('email',)   #逗号
    
    
        def no_column(self,obj):
    
            return 'xxxxxx'
    
    admin.site.register(models.UserInfo,UserAdimin)
    

    3. list_filter,列表时,定制右侧快速筛选。

    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email',)
    
        list_filter = ('name','group')
    

     还可以:

    from django.utils.translation import ugettext_lazy as _
    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email',)
    
        class Ugg(admin.SimpleListFilter):
            title = _('萨达额')
            parameter_name = 'xxxx'
    
            def lookups(self, request, model_admin):
                """
                显示筛选选项
                :param request:
                :param model_admin:
                :return:
                """
                return models.UserGroup.objects.values_list('id', 'title')
                # return models.UserGroup.filter(id__gt=10).objects.values_list('id', 'title')
                
    
            def queryset(self, request, queryset):
                """
                点击查询时,进行筛选
                :param request:
                :param queryset:
                :return:
                """
                v = self.value()
                return queryset.filter(group_id=v)
    
        list_filter = ('name',Ugg)
    

    4. list_select_related,列表时,连表查询是否自动select_related

    from django.utils.translation import ugettext_lazy as _
    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email','group')
    
        class Ugg(admin.SimpleListFilter):
            title = _('萨达额')
            parameter_name = 'xxxx'
    
            def lookups(self, request, model_admin):
                """
                显示筛选选项
                :param request:
                :param model_admin:
                :return:
                """
                return models.UserGroup.objects.values_list('id', 'title')
                # return models.UserGroup.filter(id__gt=10).objects.values_list('id', 'title')
    
    
            def queryset(self, request, queryset):
                """
                点击查询时,进行筛选
                :param request:
                :param queryset:
                :return:
                """
                v = self.value()
                if v:
                    return queryset.filter(group_id=v)
                else:
                    return request
    
        list_filter = ('group',Ugg)
        list_select_related = ('group',)
    

    5. 分页相关

    # 分页,每页显示条数
        list_per_page = 100
     
    # 分页,显示全部(真实数据<该值时,才会有显示全部)
        list_max_show_all = 200
     
    # 分页插件
        paginator = Paginator
    

    6. list_editable,列表时,可以编辑的列

    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email')
    
        list_editable = ('email',)
    

    7. search_fields,列表时,模糊搜索的功能

    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email')
    
    
        search_fields = ('name','email')
    

    8. date_hierarchy,列表时,对Date和DateTime类型进行搜索

    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email','ctime')
    
        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 按钮后返回列表。
     
    New in Django 1.10.
    

    12. save_on_top = False,详细页面,在页面上方是否也显示保存删除等按钮

    13. inlines,详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除

    class UserInfoInline(admin.StackedInline):  # TabularInline 效果不一样
        extra = 0
        model = models.UserInfo
    
    
    class UserGroupAdmin(admin.ModelAdmin):
        list_display = ('title',)
        inlines = [UserInfoInline, ]
    
    admin.site.register(models.UserGroup,UserGroupAdmin)
    

    14. action,列表时,定制action中的操作

    class UserAdimin(admin.ModelAdmin):
        list_display = ('name','email','ctime')
    
    
        def func(self, request, queryset):
            # print(self, request, queryset)
            print(request.POST.getlist('_selected_action'))
            print('代码来了。。。。')
    
        func.short_description = "中文显示自定义Actions"
        actions = [func, ]
    

    15. 定制HTML模板

    add_form_template = None
    change_form_template = None
    change_list_template = None
    delete_confirmation_template = None
    delete_selected_confirmation_template = None
    object_history_template = None


    # change_list_template = ['list.html']
  • 相关阅读:
    ASP.NET解决Sqlite日期类型问题:该字符串未被识别为有效的 DateTime
    无法定位程序输入点_except_handler4_common于动态链接库msvcrt.dll上
    无意中找到的一个“通用不间断滚动JS封装类”
    sqlite中字符串的连接操作
    dhtmlxTree介绍
    项目之小模块有感
    JAVA中创建对象的四种方式
    Eclipse插件汇集(未完善)
    apache之beanutils包比较工具类ComparatorUtils
    Eclipse之maven插件m2eclips
  • 原文地址:https://www.cnblogs.com/zhaochangbo/p/7690109.html
Copyright © 2020-2023  润新知