django使用orm實現條件搜尋和模糊匹配

2021-10-10 10:12:08 字數 3702 閱讀 2947

2. 模糊匹配

使用條件搜尋時,可以直接使用filter鏈來過濾掉滿足條件的記錄,因為每次filter()的結果仍然是乙個queryset,因此可以根據引數有無,來新增filter。

先不新增任何搜尋條件獲取乙個queryset。

invoice_heads = invoicehead.objects.

filter

( resource_uid=resource_uid,

status=

"y",

user_id=user_id,

)

if invoice_type is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_type=invoice_type)

大於等於__gte, 小於等於__lte。

if end_date is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_date__lte=end_date)

將每個需要搜尋的字段先判斷一下是不是none,如果不是none,那麼就在queryset後面在拼接.filter() ,實現條件搜尋過濾。

class

openedinvoicequery

(baseview)

:def

get(self, request, resource_uid=

none):

r = result(

) using = self.get_partner(request)

try:

student = self.student_auth(request)

user_id = student[

'id'

] invoice_type = request.get.get(

"invoice_type"

) invoice_code = request.get.get(

"invoice_code"

) invoice_no = request.get.get(

"invoice_no"

) start_date = request.get.get(

"start_date"

) end_date = request.get.get(

"end_date"

) invoice_status = request.get.get(

"invoice_status",0

) size =

int(request.get.get(

"size",10

))page =

int(request.get.get(

"page",0

))invoice_heads = invoicehead.objects.

filter

( resource_uid=resource_uid,

status=

"y",

user_id=user_id,

)if invoice_type is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_type=invoice_type)

if invoice_code is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_code=invoice_code)

if invoice_no is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_no=invoice_no)

if invoice_status is

notnone

: invoice_heads = invoice_heads.

filter

(red_flag=invoice_status)

if start_date is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_date__gte=start_date)

if end_date is

notnone

: invoice_heads = invoice_heads.

filter

(invoice_date__lte=end_date)

r.data_count =

len(invoice_heads)

page_obj = paginator(invoice_heads, size)

page_data = page_obj.get_page(page)

res = page_data.object_list

data =

for i in res:

d = invoiceheadserializers(i)

.data

line = invoiceline.objects.

filter

(head=i)

.first(

) d[

"main_goods_name"

]= line.goods_name

d["total_price_with_tax"

]= i.amount + i.total_amount

r.data = data

except exception as e:

r.error(e)

return self.s_result(r)

在django的orm中,可以使用__icontains 來表示mysql裡的like 「%%」

匹配code,key,goods_name,******_name:

from django.db.models import q  

row = invoiceproductcategories.objects.using(

"admin"

) \ .

filter

(~q(rate1=

none),

goods_name__icontains=goods_name,

******_name__icontains=******_name,

code__icontains=code,

key__icontains=key)

不等於使用~q,在預設的不等於none中,需要把~q放到filter()的最前面,要不然會編譯不通過。

Django 中ORM 的使用

1 手動新建乙個資料庫 2 告訴django連線哪個資料庫 settings.py裡配置資料庫連線資訊 資料庫相關的配置項 databases 3 告訴django用pymsql 模組代替預設的mysqldb 去連線mysql 在和settings.py 同目錄下的 init py檔案中做配置 im...

簡單示例實現django的orm

在django中如果我們定義了乙個user的model。就可以使用user.objects.create 等方法.那麼在django的原始碼中是如何實現這種寫法的呢?下面 從最外層簡單完成了django orm的呼叫方法實現.ps 下面的 只是實現了django的最外面呼叫實現而已.class ma...

使用Django的ORM詳細步驟

使用django的orm詳細步驟 1.自己動手建立資料庫 在cmd中輸入 create database 資料庫名 2.在django專案中設定連線資料庫的相關配置 告訴django連線哪乙個資料庫 資料庫相關的配置 3.告訴django用pymysql代替預設的mysqldb 連線mysql資料庫...