CRM Multi-Conditional Filtering and Paging - Day 16

- Restore content to start ---

1.1 CRM first displays the fields to be filtered

The front-end code is as follows:

 {% for condtion in admin_class.list_filters%}
     <div class="col-lg-2">
           <span>{{condtion}}</span>

 </div>

The list_filters filtered field values are defined in the class Customer(admin_class) in the kind_admin.py file, and the filtered fields are iterated through in the.html file

<div class="row">
            <form class="" method="get">
                {% for condtion in admin_class.list_filters%}
                <div class="col-lg-2">
                    <span>{{condtion}}</span>
                    {% render_filter_ele condtion admin_class filter_condtions %}
                </div>
                {% endfor %}
                <button type="SUBMIT" class="btn btn-success">retrieval</button>
            </form>
 </div>

 

1.2 CRM displays the values of filtered fields, requesting them using form forms and get

Method: 1. Request using form form and get

2. Create a new utils.py file under the app name type_admin file to hold the data requested by the front end and place the dict type in a variable named dictfilter_condtions

3. Return admin_class, filter_condtions in view.py file

4. Value display of judgment filter in custom tags.py, call of function in tags.py passed in front end

1. Request using form form and get, front-end code table_objs.html file:

<div class="row">
            <form class="" method="get">
                {% for condtion in admin_class.list_filters%}
                <div class="col-lg-2">
                    <span>{{condtion}}</span>
                    {% render_filter_ele condtion admin_class filter_condtions %}
                </div>
                {% endfor %}
                <button type="SUBMIT" class="btn btn-success">retrieval</button>
            </form>
</div>

2. Create a new utils.py file under the app name type_admin file to store the data requested by the front end in a dictfilter_condtions, uttils.py file code

This is a filter of the requested data
admin_class.model.objects.filter(**filter_conditions) is equivalent to
>>> m={"source":0}
>>> models.Customer.objects.filter(**m)
<QuerySet [<Customer: 5676567@qq.com>]>or

If there is no data in dict, all data will be filtered as follows:
>>> m={}
>>> models.Customer.objects.filter(**m)
<QuerySet [<Customer: 5676567@qq.com>, <Customer: 25064568@qq.com>, <Customer: 787874859@qq.com>, <Customer: 2165
64732@qqcom>]>

 

def table_filter(request,admin_class):
    """Conditional filtering and returning filtered data"""

    filter_conditions = {}
    for k,v in request.GET.items():
        if v:
            filter_conditions[k]=v
    print("filter_conditions:",filter_conditions)
    print("admin_class.model.objects.filter(**filter_conditions)",admin_class.model.objects.filter(**filter_conditions))
    return admin_class.model.objects.filter(**filter_conditions),filter_conditions

3. Return admin_class, filter_condtions, view.py code in view.py file as follows:

def display_table_objs(request,app_name,table_name):

    print("-->",app_name,table_name)
    #models_module = importlib.import_module('%s.models'%(app_name))
    #model_obj = getattr(models_module,table_name)
    admin_class = kind_admin.enabled_admins[app_name][table_name]
    #admin_class = king_admin.enabled_admins[crm][userprofile]

    #object_list = admin_class.model.objects.all()
    object_list,filter_condtions = table_filter(request,admin_class)
    paginator = Paginator(object_list, admin_class.list_per_page) # Show 25 contacts per page
    print("paginator------",paginator)
    page = request.GET.get('page')
    try:
        query_sets = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        query_sets = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        query_sets = paginator.page(paginator.num_pages)
    print("query_sets------------",query_sets)
    return render(request,"kindadmin/table_objs.html",{"admin_class":admin_class,
                                                        "query_sets":query_sets,
                                                        "filter_condtions":filter_condtions})

 

Partial page code is paging code

4. Display the filtered values in the custom tags.py, pass in the call to the function in tags.py at the front end, tags.py file code:

4.1 has attributes that determine whether a field in such a Customer has a choices attribute or a ForeignKey attribute.

4.2 No judgment on other attributes, resulting in no data filtering on other fields with values

If the value of the requested data is equal to the value of the looping filter box then this is the selected property selected, and no such property

Find attribute information for a field within a class
filed_obj = admin_class.model._meta.get_field(condtion) is equivalent to

>>> m = models.Customer._meta.get_field("source")
>>> m
<filed_obj.choices.db.models.fields.SmallIntegerField: source>

Is there a choices in the class attribute information: filed_obj.choices is equivalent to:

>>> m.choices
((0,'Introduction'), (1,'QQ Group', (2,'Official Network'), (3,'Baidu Promotion'),

Type(filed_obj). u name_u Lookup property has the same type as:

>>> type(m).__name__

'ForeignKey'

If it is a foreign key, use get_choices for values such as

filed_obj.get_choices()[1:]: equivalent
>>> m.get_choices()
[('', '---------'), (1, 'python')]
The previous list[0] is the title above, so cut from the list[1:]

 

 

@register.simple_tag
def render_filter_ele(condtion,admin_class,filter_condtions):
    #Background return value inside filter box
    """
    :param condtion: Field names that need to be filtered
    :param admin_class: such as Customer
    :param filter_condtions: Front End Requesting Data json
    :return:
    """
    select_ele = '''<select class="form-control" name='%s'><option value=''>----</option>'''%(condtion)
    filed_obj = admin_class.model._meta.get_field(condtion)
    #Judge if choices type
    if filed_obj.choices:
        selected=''
        for choice_item in filed_obj.choices:
            print("choice", choice_item, filter_condtions.get(condtion), type(filter_condtions.get(condtion)))
            if filter_condtions.get(condtion) == str(choice_item[0]):
                selected = "selected"

            select_ele+='''<option value='%s' %s>%s</option>'''%(choice_item[0],selected,choice_item[1])
            selected=''
    #Judging if it belongs ForeignKey Type of time
    if type(filed_obj).__name__ == 'ForeignKey':
        selected = ''
        for choice_item in filed_obj.get_choices()[1:]:
            if filter_condtions.get(condtion) == str(choice_item[0]):
                selected ="selected"

            select_ele += '''<option value='%s' %s>%s</option>''' % (choice_item[0], selected, choice_item[1])
            selected = ''

    select_ele+="<select/>"

    return mark_safe(select_ele)

The rest of the pages will be written tomorrow, and I went to bed yesterday thinking about why that page was wrong there

Keywords: PHP Attribute network Python JSON

Added by medusa1414 on Mon, 05 Aug 2019 21:42:55 +0300