Django Model Filter condition filtering, multi-table join query, reverse query, distinct of a field

Source: Internet
Author: User
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.