python whole stack development _day54_django Q, F query

from django.test import TestCase

# Create your tests here.
import os


__name__ == IF "__main__":
os.environ.setdefault ( "DJANGO_SETTINGS_MODULE", "day59.settings")
Import Django
django.setup ()
from app01 Import Models
goods sold # inquiry number is greater than 50
# res = models.Product .objects.filter (maichu__gt = 50)
# Print (RES)
# inquiry number is greater than the number of stock sold goods

Import from django.db.models F. #, Q
# F. Discover
# RES = models.Product.objects.filter (maichu__gt = F. ( 'Kucun'))
# Print (RES)
# will increase the price of all the items 100
# models.Product.objects.update (. price = F. ( '. price') + 100)
# the names of all products back plus a burst section
# Import from django.db.models.functions Concat
# Import from django.db.models the Value
# models.Product.objects.update (name = Concat (F. ( 'name'), the Value ( "Cleavage ')))

# Q Query
# res = models.Product.objects.filter (price = 188.88 , name = ' dress burst models')
# Print (RES)
# Import from django.db.models F, Q
# RES = models.Product.objects .filter (Q (price = 188.88) , Q (name = ' dress burst models')) # and
# RES = models.Product.objects.filter (Q (. price = 188.88) | Q (name = 'dress burst models' )) # or
# RES = models.Product.objects.filter (Q (. price = 188.88) | ~ Q (name = 'dress burst models')) # not
# mix should be noted that the object must be placed in an ordinary Q front filter condition
# res = models.Product.objects.filter (~ Q ( name = ' dress explosion models'), = 188.88. price) Not #
# Print (RES)

Supplementary objects # Q (******)
# Import from django.db.models F., Q
# Q = Q ()
# = q.connector 'or' # Q by this parameter and the object may be a default relationship becomes into or
# q.children.append (( '. price', 188.88))
# q.children.append (( 'name', 'high-heeled shoes burst models'))
# RES = models.Product.objects.filter (Q) # Q is the default object query and
# Print (RES)

Transaction #
# transaction the ACID
"" "
atomicity
Consistency
Isolation
Persistence
" ""
# Import from django.db Transaction
# Import from django.db.models F.
# With transaction.atomic ():
# # in the code block with children write your transactional operations
# models.Product.objects.filter (the above mentioned id = 1) .Update (kucun = F ( 'kucun') - 1)
# models.Product.objects.filter (the above mentioned id = 1) .Update (maichu F. = ( 'maichu') +. 1)
#
# # write additional code logic
# print ( 'hahah')

 

# Only defer to get an object of both opposite
# models.Product.objects.values RES = ( 'name')
# models.Product.objects.only RES = ( 'name')
# RES = Models. Product.objects.defer ( 'name')
# for I in RES:
# Print (i.name)

models.Product.objects.filter = RES (= ID. 1) .first ()
Print (res.gender)
Print (res.get_gender_display ()) # Chinese comments acquired corresponding number
# models.Product.objects.create (.. .gender = 1)

Guess you like

Origin www.cnblogs.com/xuxingping/p/11020098.html