CRM multi-criteria query - Day 20

1, the front end code is as follows:

<div class="row">
          <div class="col-lg-2">
                 <input type="search" name="_q" class="form-control" value="{{search_value}}" placeholder="search by {%for column in admin_class.search_fields %} {{column}} {% endfor %}" style="margin-left:15px">
          </div>
          <div class="col-lg-2">
                 <button type="SUBMIT" class="btn btn-success">search</button>
          </div>
</div>

 

When the front end of which the value of the search box click Next, the value of which will disappear in view.py file returns the requested data to the front end value of the values, value = "{} {} search_value"

 

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)

    #多条件搜索功能

    object_list = table_search(request,admin_class,object_list)
    #After data sorting 
    the object_list, order_key = the table_order (Request, the object_list) 

    paginator = Paginator (the object_list, admin_class.list_per_page) # Show the Contacts 25 per Page 
    Print ( " paginator ------ " , paginator) 
    Page = request.GET. GET ( ' Page ' )
     the try : 
        query_sets = paginator.page (Page)
     the except PageNotAnInteger:
         # the If not Page iS AN Integer, the deliver First Page. 
        query_sets = paginator.page (1) # # this is your data be paged The value 
    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,
                                                       "order_key":order_key,
                                                       "previous_orderkey":request.GET.get("o",""),
                                                       "search_value":request.GET.get("_q","")})

 

 

 

When I click Next, statistical data does not give the value of the search also added links to pages inside stitching, page up and down is the same

Front-end plus placeholder told that several users can use the search query field, the value should be removed from circulation admin_class in the search_fields

 

2, the search query is a query after filtration, increase in view.py in:

 

the object_list, filter_condtions = table_filter (Request, admin_class) 

    # multiple search conditions 

    the object_list = table_search (Request, admin_class, the object_list)
     # after the sort data 
    object_list, order_key = table_order (request, object_list)

table_order function in utils.py file:

_Q get over the value of the request, and then use the Q () or to inquire, at first I did not judge search_value, led me to re-enter the pages are not able to say None query:

 

 

#Q用法
from django.db.models import Q

>>> from PerfectCRM.Perfectcrm.crm import models
>>> from django.db.models import Q
>>> models.Customer.objects.values('qq','name')
<QuerySet [{'qq': '[email protected]', 'name': '江大可'}, {'qq': '[email protected] ' , ' name ' : ' Jiang cocoa ' }, { ' QQ ' : ' 78 787 
[email protected] ' , ' name ' : ' Yang ' }, { ' QQ ' : ' 216 564 732 @ qqcom ' , ' name ' : ' King greatly ' }, { ' QQ ' : ' [email protected] ' ,'name': '
刘新'}]>
>>> con = Q()
>>> con.connector="OR"
>>> con.children.append(('qq__contains','5676567'))
>>> con.children.append(('name__contains','江大')) #name包函的内容
>>> con
<Q: (OR: ('qq__contains', '5676567'), ('name__contains', '江大'))>
>>> models.Customer.objects.values('qq','name').filter(con)
<QuerySet [{'qq': '[email protected]', 'name': '江大可'}]>
>>>

 

def table_search(request,admin_class,objs):
    """
    多条件搜索功能
    :param request: 请求的
    :param admin_class: Customer
    :param objs: 筛选过后的对象
    :return:
    """
    search_value = request.GET.get("_q","")
    if search_value:
        con = Q()
        con.connector="OR"
        for column in admin_class.search_fields:
            con.children.append(('%s__contains'%column, search_value)) 

        he = objs.filter (con)
     else : 
        he = objs 

    return it

 

 When submitted _q also as a filter, it will lead to error, it is resolved:

DEF table_filter (Request, admin_class):
     "" " data will be filtered and returned filtration conditions " "" 

    filter_conditions = {}
     for K, V in request.GET.items (): 
        kitem = [ ' Page ' , ' O ' , ' _q ' ]
         IF K in kitem:
             Continue 
        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

 

 

  

 

Guess you like

Origin www.cnblogs.com/venvive/p/11330302.html