美文网首页Python Web开发学习
【CRM客户关系管理】21.action功能开发,默认actio

【CRM客户关系管理】21.action功能开发,默认actio

作者: 吾星喵 | 来源:发表于2018-12-23 15:57 被阅读0次

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,在<table>...上方增加执行表单

<form>
    <div class="row">
        <div class="col-md-3">
            <select class="form-control" name="action">
                <option value="">-------</option>
                {% for action in admin_class.actions %}
                    <option value="{{ action }}">{{ action }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-md-2">
            <input type="submit" value="执行" class="btn btn-primary">
        </div>
    </div>
</form>

<table class="table table-striped">
    ......
</table>
image.png

表格内容增加勾选框

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

<tbody>
    {% for obj in queryset %}
        <tr>
            <td><input row-select="true" type="checkbox" value="{{ obj.id }}"></td>
            {% build_table_body obj admin_class %}
            <td><a href="{% url 'djadmin:table_delete' app_name model_name obj.id %}">删除</a></td>
        </tr>
    {% endfor %}
</tbody>

表格头部增加勾选框

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

<thead>
    <tr>
        <th><input type="checkbox" onclick="SelectALLObjs(this)"></th>
        {% if admin_class.list_display %}
            {% for display_field in admin_class.list_display %}
                <th>
                    <a href="?_order={% get_sorted_data display_field current_order_field forloop.counter0 %}{% render_filter_args admin_class %}">
                        {{ display_field }} {% get_sorted_arrow display_field current_order_field forloop.counter0 %}
                    </a>
                </th>
            {% endfor %}
        {% else %}
            {% build_table_head_name admin_class %}
        {% endif %}
        <th>操作</th>
        <!--
        {% build_table_head_name admin_class %}
        -->
    </tr>
</thead>

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

<script>
    function SelectALLObjs(ele) {
        if ($(ele).prop('checked')){
            $('input[row-select]').prop('checked',true)
        }else{
             $('input[row-select]').prop('checked',false)
        }
    }

</script>
复选框复选框

相关文章

网友评论

    本文标题:【CRM客户关系管理】21.action功能开发,默认actio

    本文链接:https://www.haomeiwen.com/subject/jwsnkqtx.html