1. Multi-table join query: When I knew this, I suddenly thought Django was too NX.
Class A (models. Model ):
Name = models. charfield (u'name ')
Class B (models. Model ):
AA = models. foreignkey ()
B. Objects. Filter (AA _ name _ contains = 'searchtitle ')
1.5 I called it reverse query, and then inserted the record 1.5. When I knew it, I instantly felt that Django was too Mrs. NX.
Class A (models. Model ):
Name = models. charfield (u'name ')
Class B (models. Model ):
AA = models. foreignkey (A, related_name = "fan ")
BB = models. charfield (u'name ')
Check A:. objects. filter (fan _ BB = 'xxxx'), all know the role of related_name,. fan. all () is a group of B instances with a foreign key. The previous usage is to query all (B. AA = A and B. BB = XXXX), and then you can use the _ relationships to find the instance !!!
2. When queryset is selected as the condition, filter indicates = and exclude indicates! =.
Queryset. Distinct () deduplication
_ Exact is exactly like 'aaa'
_ Iexact is exact equal to ignoring the upper and lower case Ilike 'aaa'
_ Contains include like '% AAA %'
_ Icontains indicates that Ilike '% AAA %' is case-insensitive. However, for SQLite, the effect of contains is equivalent to that of icontains.
_ GT greater
_ GTE greater than or equal
_ Lt is less
_ Equal to or less than LTE
_ In exists in a list range.
_ Startswith starts...
_ Istartswith starts with... and ignores case sensitivity.
_ Endswith ends...
_ Iendswith ends with..., case insensitive
_ Range is within the range...
_ Year Year of the Date Field
_ Month
_ Day of the Date Field
_ Isnull = true/false
Example:
> Q1 = entry. Objects. Filter (headline _ startswith = "what ")
> Q2 = q1.exclude (pub_date _ GTE = datetime. Date. Today ())
> Q3 = q1.filter (pub_date _ GTE = datetime. Date. Today ())
>>> Q = Q. Filter (pub_date _ LTE = datetime. Date. Today ())
>>> Q = Q. exclude (body_text _ icontains = "food ")
That is, q1.filter (pub_date _ GTE = datetime. date. today () is expressed as time> = now, q1.exclude (pub_date _ GTE = datetime. date. today () is represented as <= now
Supplement:
"Get the distinct value of a field in Django models ". Select distinct XXX from table_name. When values is used, valuesqueryset is generated (such as a list composed of N dict). It is assumed that the performance of big data is not affected. After all, the queryset series are used for query operations.
XXXX. Objects. Values ("field_name"). Distinct ()
# Or
XXXX. Objects. Distinct (). Values ("field_name ")
The two statements generate the same SQL statement. original post address: http://blog.csdn.net/tsbob/article/details/1340293.
About cache:
Queryset is cached. A =. objects. all (), print [I for I in a]. the first print operation queries the database, and the result is saved in the built-in cache of queryset. When print is executed, the result is taken from the cache.
In many cases, you only need to judge whether queryset is null. if queryset: Pass 2.if queryset. count> 0: Pass 3.If queryset. exists (): Pass. the performance of these three methods is improved in sequence.
When queryset is very large, the cache will become a problem. At this time, queryset. iterator () can be used. The usefulness of the iterator is not much discussed. It can be used according to specific requirements.