【CRM客户关系管理】21.action功能开发,默认action,生成自己的action

DjangoAdmin后台增加actions功能

image.png

Django admin默认的执行,选中后,可以通过执行批量删除,也可以自定义一个函数去执行批量操作

修改crm/admin.py文件,增加CustomerInfoAdmin(admin.ModelAdmin)类的actions

class CustomerInfoAdmin(admin.ModelAdmin):
    list_display = ['name', 'contact_type', 'contact', 'consultant', 'consult_content', 'status', 'created_time']
    list_filter = ['source', 'consultant', 'status', 'created_time']
    search_fields = ['contact', 'consultant__name', 'consult_content']
    readonly_fields = ['contact', 'status']
    filter_horizontal = ['consult_courses']
    actions = ['change_status']

    def change_status(self, request, queryset):
        queryset.update(status=3)  # 批量设置可装状态为“结业”

    change_status.short_description = '客户状态修改为结业'  # 设置后台可见描述


admin.site.register(CustomerInfo, CustomerInfoAdmin)
image.png
image.png

选中后执行,就会把所有的状态修改为结业。

djadmin actions功能

父类BaseDjAdmin增加actions属性

修改djadmin/djadmin_base.py

# djadmin/djadmin_base.py


class BaseDjAdmin(object):
    list_display = []
    list_filter = []
    search_fields = []
    readonly_fields = []
    filter_horizontal = []
    list_per_page = 5
    actions = []

自定义crm的djadmin

可以直接将admin.py的属性和方法复制过来

class CustomerInfoAdmin(BaseDjAdmin):  # 不使用object,直接继承BaseDjAdmin
    list_display = ['name', 'contact_type', 'contact', 'consultant', 'consult_content', 'status', 'created_time']
    list_filter = ['source', 'consultant', 'status', 'created_time']
    search_fields = ['contact', 'consultant__name', 'consult_content']
    readonly_fields = ['contact', 'status']
    filter_horizontal = ['consult_courses']
    actions = ['change_status']

    def change_status(self, request, queryset):
        queryset.update(status=3)  # 批量设置可装状态为“结业”

    change_status.short_description = '客户状态修改为结业'  # 设置后台可见描述


site.register(models.CustomerInfo, CustomerInfoAdmin)

djadmin页面显示actions

增加执行按钮表单

修改table_detail.html,在

...上方增加执行表单

......
image.png

表格内容增加勾选框

修改table_detail.html的增加一行td,设置为复选框


    {% for obj in queryset %}
        
            
            {% build_table_body obj admin_class %}
            删除
        
    {% endfor %}

表格头部增加勾选框

修改table_detail.html的增加一行th,设置为复选框


    
        
        {% if admin_class.list_display %}
            {% for display_field in admin_class.list_display %}
                
                    
                        {{ display_field }} {% get_sorted_arrow display_field current_order_field forloop.counter0 %}
                    
                
            {% endfor %}
        {% else %}
            {% build_table_head_name admin_class %}
        {% endif %}
        操作
        
    

还需要增加js代码,可以权限/取消全选下方表格内容的勾选,在{% endblock %}标签前面增加


复选框

你可能感兴趣的:(【CRM客户关系管理】21.action功能开发,默认action,生成自己的action)