F Q查詢,事務以及only與defer

2022-07-28 06:06:11 字數 3655 閱讀 8684

在models檢視中建立列表

class

product(models.model)

name = models.charfield(max_length=32)

price = models.decimalfield(max_digits=8, decimal_places=2)

maichu =models.integerfield()

kucun = models.integerfield()

def __str__(self)

return '商品物件的名字:%s'%self.name

在mysql中建立表

# 查詢賣出數大於50的商品

res = models.product.objects.filter(maichu__get=50)

print(res)

#查詢賣出數大於庫存數的商品。

這個時候就要用到拿到所有的賣出數,庫存數,沒有具體的條件,現在就要用到f與q查詢

f查詢

f:能夠幫助獲取到某乙個字段對應的值

from

django.db.models inport f,q

res = models.product.objects.filter(maichu__gt=f('

kucun'))

print(res)

# 將所有商品的**提高100塊(這句話說的就是增刪改查中的改資料,首先要拿到原**,再在原來的**上加100)

models.product.objects.update(price=f('

price

')+100)

# 將所有商品名稱後面都加乙個爆款(可能很多小夥伴都想到了用join去做字串的拼接,但是django是不支援這種格式的)

from django.db.models.functions import

concat

from django.db.models import

value

models.product.objects.update(name=concat(f('

name

'),value('

爆款')))

q查詢為什麼要用到q查詢?

因為在filter中條件都是and關係,這裡想讓裡面的條件關係變成or,所以就用到了q

from

django.db.models inport f,q

res = models.product.objects.filter(q(price=188.88),q(name='

l連衣裙爆款

')) #

andres = models.product.objects.filter(q(price=188.88)|q(name='

l連衣裙爆款

')) #

orres = models.product.objects.filter(q(price=188.88)|~q(name='

l連衣裙爆款

')) #

notprint(res)

混合使用:

q如果要和關鍵字條件混合使用,q必須在前

res = models.product.objects.filter(~q(name='

連衣裙爆款

'),(price=188.88))

print(res)

字串轉成變數名的name

from django.db.models import

f, q

q =q()

q.connector = 'or'

#通過這個引數可以將q物件預設的and關係變成or

price

',188.88))

'name

','高跟鞋爆款'))

res = models.product.objects.filter(q) #

q物件查詢預設也是and

print(res)

事務四大特性(acid):原子性,一致性,隔離性,永續性

from django.db import

transaction

from django.db.models import

fwith transaction.atomic():

在with**塊兒寫你的事務操作

models.product.objects.filter(id=1).update(kucun=f('

kucun

')-1)

models.product.objects.filter(id=1).update(maichu=f('

maichu

')+1)

寫其他**邏輯

print('

hahaha

')

django自定義char型別在models中定義類

class

mycharfield(models.field):

def__init__(self,max_length,*args,**kwargs):

self.max_length =max_length

super().

__init__(max_length=max_length,*args,**kwargs)

defdb_type(self, connection):

return

'char(%s)

'%self.max_length

class

product(models.model)

name = models.charfield(max_length=32) #

都是類例項化出來的物件

price = models.decimalfield(max_digits=8, decimal_places=2)

maichu =models.integerfield()

kucun =models.integerfield()

info = mycharfield(max_length=32,null=true) #

該欄位可以為空

再執行makemigrations    migrate

only與defer

#

拿到的是乙個物件 兩者是相反的

res = models.product.objects.values('

name')

res = models.product.objects.only('

name')

res = models.product.objects.defer('

name')

for i in

res:

print(i.name)

Django之F Q查詢,事務,自定義char欄位

f查詢 from django.db.models import f,q 當查詢條件來自於資料庫的某個字段,這個時候就必須使用f 查詢賣出數大於庫存數的商品 res models.product.objects.filter maichu gt f kucun 將所有商品的 提高100塊 model...

MySql多表查詢 事務

1.準備sql 建立部門表 create table dept id intprimary keyauto increment,name varchar 20 建立員工表 create table emp id intprimary keyauto increment,name varchar 10...

mysql查詢事務和鎖

記錄原因 今天在乙個mysql更新語句的執行過程中,總是執行超時,後來查到原因是因為有乙個事務沒有關閉,導致那條記錄的查詢和更新都會執行超時 以下的sql語句可以查詢當前資料庫,有哪些事務,都鎖定哪些資源 select trx id as 事務id trx state as 事務狀態 trx req...