CRM多條件查詢 第20天

2022-04-29 01:33:09 字數 4311 閱讀 6818

1、前端**如下:

<

div

class

="row"

>

<

div

class

="col-lg-2"

>

<

input

type

="search"

name

="_q"

class

="form-control"

value

="}"

placeholder

="search by } "

style

="margin-left:15px"

>

div>

<

div

class

="col-lg-2"

>

<

button

type

="submit"

class

="btn btn-success"

>search

button

>

div>

div>

def

print("

-->

" ##

model_obj = getattr(models_module,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)

#排序過後的資料

object_list,order_key =table_order(request,object_list)

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

",)

前端加placeholder 告訴使用者可以使用那幾個字段搜尋查詢,值應該從admin_class裡的search_fields迴圈取出

2、搜尋查詢也是在過濾後查詢的,在view.py裡增加:

object_list,filter_condtions =table_filter(request,admin_class)

#多條件搜尋功能

object_list =table_search(request,admin_class,object_list)

#排序過後的資料

object_list,order_key = table_order(request,object_list)

table_order函式是在utils.py檔案中:

獲取請求過來的_q值,然後利用q()去或查詢,起初我沒有判斷search_value,導致我重新進入頁面說none是不能夠查詢的:

#

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')

'qq': '

[email protected]

', '

name

': '

江大可'}, , , ,

]>

>>> con =q()

>>> con.connector="or"

qq__contains

','5676567'))

name__contains

','江大

')) #name包函的內容

>>>con

'qq__contains

', '

5676567

'), ('

name__contains

', '

江大'))>

>>> models.customer.objects.values('

qq','

name

').filter(con)'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",""

)

ifsearch_value:

con =q()

con.connector="or"

for column in

admin_class.search_fields:

'%s__contains

'%column,search_value))

ele =objs.filter(con)

else

: ele =objs

return ele

提交的時候_q也會做為乙個過濾條件,所以也會導致報錯,解決是:

def

table_filter(request,admin_class):

"""進行條件過濾並返回過濾後的資料

"""filter_conditions ={}

for k,v in

request.get.items():

kitem =['

page

','o

','_q']

if k in

kitem:

continue

ifv:

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

多條件查詢

思路 1.獲取引數值 2.生成查詢條件 3.獲取查詢結果 4.繫結查詢選項 呼叫geturlhtml方法生成查詢url 例如 var y2013 pnvshihufu qbeijing sxuhuiqu 得到url變數值 protected string geturlval string name ...

多條件查詢

多條件查詢時在做專案的時候不可缺少的功能,雖然很簡單,但是自己還是記一下,對自己有用 查詢事件 private void button查詢 click object sender,eventargs e initial catalog uid pwd this.dataserver,this.dat...

多條件查詢

開發工具與關鍵技術 vs c 當使用者需要通過一定的條件進行範圍查詢的時候,在控制器中,就需要判斷使用者傳來的條件,一般使用者條件查詢會有下拉框,文字框等from表單元件 預設下拉框內的資料已繫結,本文暫不進行時間的範圍查詢 通過獲取這些元件的值,可以方便快捷的採集使用者的資訊 一 查詢 創鍵多條件...