Django dry papers: F Q queries and inquiries and little-known operation

Query F

In the school the previous example, we construct the filter field values are just one of our own set of constant comparison.
If we want to compare the values of two fields, then how to do it?
Django provides F () to do such a comparison. Examples F. () Can be referenced in the query field, the value to compare different fields of the same model instance two.

Example 1: Query the stock number is greater than the number of sold commodities
from django.db.models Import F.
RET1 = models.Product.objects.filter (maichu__gt = F. ( 'Kucun'))
Print (RET1)

F can help us get to the table a field corresponding values ​​as my filter criteria, rather than I think a custom constant conditions, and to achieve the effect of dynamic comparison.

Django supports F (, and () operation between the object and the addition, subtraction modulo F constant and between objects). This may be based on the value type of mathematical operations in the table, will increase the price of each item 50.
models.Product.objects.update (price = F ( 'price ') + 50)
extended:
If you want to modify char field supposed to (do not spend the face value of the type of operation !!!)?
Such as: the title plus all subsequent 'new', (time required for this string Concat splicing operation, and to add value splicing Value).
Import Concat django.db.models.functions from
from the Value django.db.models Import
RET3 = models.Product.objects.update (name = Concat (F. ( 'name'), the Value ( 'new')))
Concat representation splicing operation, the parameters of the string determines the splicing head or splicing in the tail splicing, value which is spliced to the new value.

Q query

filter () method such as comma-separated relationship with the conditions. If you need to perform more complex queries (such as OR statements), you can use Q objects.

Example 1: 100 or greater than the number of queries selling price of less than 100
from django.db.models Import Q
models.Product.objects.filter (Q (maichu__gt = 100) | Q (price__lt = 100))
on the condition of one package when Q, filter and to support the cross comparison operators.

Example 2: Query and sell inventory item number 100 is not zero
models.Product.objects.filter (Q (kucun = 100) & ~ Q (maichu = 0))
We can combine and & | operator and parentheses group Q write arbitrarily complex objects.
Meanwhile, Q ~ objects can be negated, which allows a combination of normal and inverted query (NOT) query.

Example 3: The query contains new product name and the stock number is greater than 60
models.Product.objects.filter (Q (kucun__gt = 60) , name__contains = " new")
query Q can be mixed function objects and keyword parameters. All parameters (keyword arguments or Q objects) are provided to the query function will be "AND" together. However, if the Q object appears, it must precede all keyword arguments.

Affairs

The definition of the transaction: a plurality of operating sql statement into atomic operation, either while successful, a failure which is rolled back to the original state, to ensure the integrity and consistency of the data (for the NoSQL database transaction is supported portion) .
Transaction #
# buy old boy with learning Linux book
# thing to do at the database level
# 1. Create an order data
# 2 to the product will sell table number +1 inventory -1
from django.db. F Import Models
from django.db Import transaction
# open transaction
the try:
with transaction.atomic ():
# create an order data
models.Order.objects.create (NUM = "110 110 111", product_id = 1, COUNT = 1)
# can be performed successfully
models.Product.objects.filter (= ID. 1) .Update (kucun = F. ( "kucun") -. 1, maichu = F. ( "maichu") +. 1)
the except Exception AS E:
Print (E)

Other lesser-known operations

Django ORM execute native SQL

Conditions assumptions: Take the Garden blog, for example, we write the blog not to sub-file according to date, but according to points of years, and our DateField time format is the date the form, which means that we need to once again time to get processing data in a database format that we want to get the time format from this demand, Django is to provide a method did not give us, we need to write their own processing a statement.

ORM method of implementation of native SQL

extra

Continue sub statement on the basis of QuerySet

extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)

and select a set of select_params, where params is a group and, tables from which table is provided for

Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,))

Entry.objects.extra(where=['headline=%s'], params=['Lennon'])

Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])

Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid'])

举个例子:
models.UserInfo.objects.extra(
select={'newid':'select count(1) from app01_usertype where id>%s'},
select_params=[1,],
where = ['age>%s'],
params=[18,],
order_by=['-age'],
tables=['app01_usertype']
)
"""
select
app01_userinfo.id,
(select count(1) from app01_usertype where id>1) as newid
from app01_userinfo,app01_usertype
where
app01_userinfo.age > 18
order by
app01_userinfo.age desc
"""

Native SQL execution

Way more flexibility in the implementation of native SQL statements

from django.db import connection, connections

cursor = connection.cursor() # cursor = connections['default'].cursor()

cursor.execute("""SELECT * from auth_user where id = %s""", [1])

row = cursor.fetchone()

QuerySet method Daquan

Some of the more important ways:
the difference update () and save () of
both modifications to the data of the save operation, but save () function is a data column all the data items of all the re-write it again, and update () is It is updated for the efficiency of the modified terms for the high consuming less. So after modification of data saved with the update ().
select_related and prefetch_related
DEF select_related (Self, Fields *)
performance-related: even for table join operation between tables, acquiring data associated with the disposable.

总结:
1. select_related主要针一对一和多对一关系进行优化。
2. select_related使用SQL的JOIN语句进行优化,通过减少SQL查询的次数来进行优化、提高性能。

def prefetch_related (self, * lookups)
performance-related: even when multi-table table operations will be slower, use its SQL query is executed multiple times to achieve even operating table in the Python code.
Summary:
1. For many to many fields (ManyToManyField) and many fields, may be used prefetch_related () to optimize.
2. prefetch_related () method is separately optimized query each table, and then deal with their relationship with Python.

bulk_create bulk insert data
requirements: one inserts a plurality of data
Data = [ "" .join ([STR (the random.randint (65, 99)) for I in Range (. 4)]) for J in Range (100)]
obj_list = [models.A (name = I) for I in Data]
models.A.objects.bulk_create (obj_list)

Daquan

PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW QUERYSET

All DEF (Self)
# retrieve all data objects

filter DEF (Self, * args, ** kwargs)
# query condition
# condition can be: parameters, dictionary, Q

the exclude DEF (Self, * args, ** kwargs)
# query condition
# condition can be: parameters, dictionary, Q

def select_related (self, * fields)
performance-related: even for table join operation between tables, acquiring data associated with the disposable.

总结:
1. select_related主要针一对一和多对一关系进行优化。
2. select_related使用SQL的JOIN语句进行优化,通过减少SQL查询的次数来进行优化、提高性能。

def prefetch_related (self, * lookups)
performance-related: even when multi-table table operations will be slower, use its SQL query is executed multiple times to achieve even operating table in the Python code.

总结:
1. 对于多对多字段(ManyToManyField)和一对多字段,可以使用prefetch_related()来进行优化。
2. prefetch_related()的优化方式是分别查询每个表,然后用Python处理他们之间的关系。

Annotate DEF (Self, args *, ** kwargs)
# polymerizable group by for implementing queries
from django.db.models import Count, Avg, Max , Min, Sum

v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id'))
# SELECT u_id, COUNT(ui) AS `uid` FROM UserInfo GROUP BY u_id
v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id')).filter(uid__gt=1)
# SELECT u_id, COUNT(ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1
v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id',distinct=True)).filter(uid__gt=1)
# SELECT u_id, COUNT( DISTINCT ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1

distinct DEF (Self, * FIELD_NAMES are)
# for distinct deduplication
models.UserInfo.objects.values ( 'NID') distinct ().
# NID from the SELECT distinct UserInfo
Note: only available in PostgreSQL distinct de-heavy

def order_by(self, *field_names)
# 用于排序
models.UserInfo.objects.all().order_by('-id','age')

def extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)
# 构造额外的查询条件或者映射,如:子查询
Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,))
Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])
Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid'])

Reverse DEF (Self):
# reverse
models.UserInfo.objects.all () ORDER_BY ( '- NID') Reverse ()..
# Note: If there order_by, reverse is the reverse, if a plurality of the eleven descending sort

the defer DEF (Self, Fields *):
models.UserInfo.objects.defer ( 'username', 'ID')
or
. models.UserInfo.objects.filter (...) the defer ( 'username', 'ID')
# mapping data in a column negative

only DEF (Self, Fields *):
# only take data in a table
models.UserInfo.objects.only ( 'username', 'id ')
or
models.UserInfo.objects.filter (...) only (. 'username', 'id')

def using (self, alias):
Specifies the database alias parameters (set in the setting)

PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS

def raw(self, raw_query, params=None, translations=None, using=None):
# 执行原生SQL
models.UserInfo.objects.raw('select * from userinfo')

# 如果SQL是其他表时,必须将名字设置为当前UserInfo对象的主键列名
models.UserInfo.objects.raw('select id as nid from 其他表')


# 为原生SQL设置参数
models.UserInfo.objects.raw('select id as nid from userinfo where nid>%s', params=[12,])


# 将获取的到列名转换为指定列名
name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'}
Person.objects.raw('SELECT * FROM some_other_table', translations=name_map)


# 指定数据库
models.UserInfo.objects.raw('select * from userinfo', using="default")
################### 原生SQL ###################
from django.db import connection, connections
cursor = connection.cursor()  # cursor = connections['default'].cursor()
cursor.execute("""SELECT * from auth_user where id = %s""", [1])
row = cursor.fetchone() # fetchall()/fetchmany(..)

values DEF (Self, Fields):
# acquiring per-line data dictionary format
DEF values_list (Self,
Fields, kwargs **):
# acquiring per-line data tuples

a dates DEF (Self, field_name, kind, the Order = 'ASC'):
# be based on a certain part of time to re-locate and intercept specified content
# kind can only be: "year" (years), "month" (year - month ), "day" (year - month - day)
# the Order can only be: "ASC" "DESC"
# time and get the converted
- year: Year -01-01
- month the: Year - month -01
- day: year month day

models.DatePlus.objects.dates('ctime','day','DESC')

DEF DateTimes (Self, FIELD_NAME, kind, Order = 'the ASC', tzinfo = None):
# for a certain part on time to re-locate and taken to specify the content, converts the time into specified Time
# kind only "year" , "month The", "Day", "hour", "minute", "SECOND,"
# the Order can only be: "ASC" "DESC"
# tzinfo time zone objects
models.DDD.objects.datetimes ( 'ctime', 'hour ', tzinfo = pytz.UTC)
models.DDD.objects.datetimes (' the ctime ',' hour ', tzinfo = pytz.timezone (' Asia / of Shanghai '))

"""
pip3 install pytz
import pytz
pytz.all_timezones
pytz.timezone(‘Asia/Shanghai’)
"""

none DEF (Self):
# Empty QuerySet object

METHODS THAT DO DATABASE QUERIES

Aggregate DEF (Self, args *, ** kwargs):
# aggregation function, obtaining dictionary type polymerization results
from django.db.models Import the Count, Avg., Max, Min, the Sum
Result = models.UserInfo.objects.aggregate (K = COUNT ( 'U_ID', DISTINCT = True), n-COUNT = ( 'NID'))
===> { 'K':. 3, 'n-':}. 4

COUNT DEF (Self):
# Gets the number of
DEF GET (Self, * args, kwargs):
# get a single object
DEF the Create (Self,
kwargs):
# create objects
DEF bulk_create (Self, objs, batch_size = None):
# Batch insert
# batch_size represents a number of inserted
OBJS = [
models.DDD (name = 'R11'),
models.DDD (name = 'R22')
]
models.DDD.objects.bulk_create (OBJS, 10)

get_or_create DEF (Self, Defaults = None, ** kwargs):
# If present, acquisition, or else to create
the time specified # defaults created, the values of other fields of
obj, created = models.UserInfo.objects.get_or_create (username = 'root1 ', defaults = {' email ' :' 1111111 ',' u_id ': 2,' t_id ': 2})

update_or_create DEF (Self, Defaults = None, ** kwargs):
# If present, update, or create
additional fields to specify the time when the # defaults to create or update
obj, created = models.UserInfo.objects.update_or_create (username = ' root1 ', defaults = {' email ':' 1111111 ',' u_id ': 2,' t_id ': 1})

First DEF (Self):
# Get the first
DEF Last (Self):
# Gets the last

in_bulk DEF (Self, ID_LIST = None):
# primary key ID for lookup
ID_LIST = [11,21,31]
models.DDD.objects.in_bulk (ID_LIST)

the Delete DEF (Self):
# delete
DEF Update (Self, ** kwargs):
# update
DEF EXISTS (Self):
# Are there results

Guess you like

Origin www.cnblogs.com/bladecheng/p/11545189.html