码迷,mamicode.com
首页 > 其他好文 > 详细

CRM项目实战-kingadmin根据list-display配置生成的数据

时间:2019-08-08 23:10:26      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:mod   eve   inf   lib   ant   xxx   queryset   mode   ==   

a.get_status_display()  获取choice对应的值

models.User._meta.fields  获取model所有的字段对象

models.User._meta.get_field()  获取某个字段的对象

field.get_internal_type()  获取某个字段对象的字段类型

对象 b.replace()  替换初始化数据

技术图片
from kingadmin.sites import site
from kingadmin.admin_base import BaseKingAdmin
from crm import models
print(crm kingadmin ............)

class CustomerAdmin(BaseKingAdmin):
    list_display = [name,source,contact_type,contact,consultant,consult_content,status,date]
    list_filter = [source,consultant,status,date]
    search_fields = [contact,consultant__name]

site.register(models.CustomerInfo,CustomerAdmin)
site.register(models.Role)
site.register(models.Menus)
site.register(models.UserProfile)
kingadmin.py
技术图片
{% extends ‘kingadmin/index.html‘ %}
{% load kingadmin_tags %}

{% block  right-content-container %}
<h2 class="page-header">app</h2>

<div>

        {{ querysets }}

        <div>
            <form>

                {% for filter_column in admin_class.list_filter %}
                   {% build_filter_ele filter_column admin_class %}
                {% endfor %}

                <input type="submit" value="过滤">
            </form>

        </div>

        <table class="table table-striped">
            <thead>
                <tr>
                    {% for column in admin_class.list_display %}
                        <th>{{ column }}</th>
                    {% endfor %}
                </tr>
            </thead>
            <tbody>
                {% for obj in querysets %}
                    <tr>{%  build_table_row obj admin_class %}</tr>
                {% endfor %}
            </tbody>


        </table>

</div>

{% endblock %}
table_obj_list.html
技术图片
from django.template import Library
from django.utils.safestring import mark_safe
import datetime ,time
register = Library()

@register.simple_tag
def build_filter_ele(filter_column,admin_class):

    column_obj = admin_class.model._meta.get_field(filter_column)
    print("column obj:",column_obj)
    try:
        filter_ele = "<select name=‘%s‘>" % filter_column
        for choice in column_obj.get_choices():
            selected = ‘‘
            if filter_column in admin_class.filter_condtions:#当前字段被过滤了
                # print("filter_column", choice,
                #       type(admin_class.filter_condtions.get(filter_column)),
                #       admin_class.filter_condtions.get(filter_column))
                if str(choice[0]) == admin_class.filter_condtions.get(filter_column):#当前值被选中了
                    selected = selected
                    print(selected......)

            option = "<option value=‘%s‘ %s>%s</option>" % (choice[0],selected,choice[1])
            filter_ele += option
    except AttributeError as e:
        print("err",e)
        filter_ele = "<select name=‘%s__gte‘>" % filter_column
        if column_obj.get_internal_type() in (DateField,DateTimeField):
            time_obj = datetime.datetime.now()
            time_list = [
                [‘‘,------],
                [time_obj,Today],
                [time_obj - datetime.timedelta(7),七天内],
                [time_obj.replace(day=1),本月],
                [time_obj - datetime.timedelta(90),三个月内],
                [time_obj.replace(month=1,day=1),YearToDay(YTD)],
                [‘‘,ALL],
            ]

            for i in time_list:
                selected = ‘‘
                time_to_str = ‘‘if not i[0] else  "%s-%s-%s"%(i[0].year,i[0].month,i[0].day)
                if  "%s__gte"% filter_column in admin_class.filter_condtions:  # 当前字段被过滤了
                    print(-------------gte)
                    if time_to_str == admin_class.filter_condtions.get("%s__gte"% filter_column):  # 当前值被选中了
                        selected = selected
                option = "<option value=‘%s‘ %s>%s</option>" %                          (time_to_str ,selected,i[1])
                filter_ele += option

    filter_ele += "</select>"
    return mark_safe(filter_ele)



@register.simple_tag
def  build_table_row(obj,admin_class):
    """生成一条记录的html element"""

    ele = ""
    for column_name in admin_class.list_display:

        column_obj = admin_class.model._meta.get_field(column_name)
        if column_obj.choices: #get_xxx_display
            column_data = getattr(obj,get_%s_display% column_name)()
        else:
            column_data = getattr(obj,column_name)

        td_ele = "<td>%s</td>"% column_data
        ele += td_ele

    return mark_safe(ele)
kingadmin_tags.py

技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片

CRM项目实战-kingadmin根据list-display配置生成的数据

标签:mod   eve   inf   lib   ant   xxx   queryset   mode   ==   

原文地址:https://www.cnblogs.com/jintian/p/11324350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!