Django Model filter Conditional filtering, and multi-table join query, reverse query, a field of distinct

Source: Internet
Author: User
Tags datetime

Transferred from:

1. Multi-table Connection query: When I know this, I feel that Django is too nx. class A (models. Model):
Name = models. Charfield (U ' name ')
Class B (models. Model):

AA = models. ForeignKey (A)

# AA = models. ForeignKey (A, Primarykey=true) #注: In a real database table structure, the name of the corresponding field is added by Django with a _id suffix, which becomes aa_id B.objects.filter (aa__name__ contains= ' Searchtitle ')

1.5 I call it reverse query, then insert record 1.5, when I know the moment I feel that Django too wife 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:a.objects.filter (fan__bb= ' XXXX '), are aware of the role of Related_name, () is a group of a as a foreign key B instance, can be used before such a usage is to query out all (B.aa=a and An instance of a, and can also be found through the various relationships, true red shock ...

2. When the condition is selected Queryset, filter indicates that =,exclude represents! =.
Queryset.distinct () to repeat
__exact exactly equals like ' AAA '
__iexact precision equals ignoring case ilike ' AAA '
__contains contains like '%aaa% '
__icontains includes ignoring case ilike '%aaa% ', but for SQLite, the effect of contains is equivalent to Icontains.
__GT Greater than
__gte greater than or equal to
__lt less than
__lte less than or equal to
__in exists in a list range
__startswith to ... Beginning
__istartswith to ... Start ignoring case
__endswith to ... End
__iendswith to ... End, ignoring case
__range in ... Within range
__year Year of Date field
__month Month of Date field
Day of the __day date field

>> q1 = Entry.objects.filter (headline__startswith= "what")
>> q2 = q1.exclude ( ())
>> q3 = Q1.filter ( ())
>>> q = Q.filter ( ())
>>> q = q.exclude (body_text__icontains= "food")

That is, Q1.filter ( ()) is expressed as time >=now,q1.exclude ( ()) expressed as <=now

2013/12/12 Supplement:
"Get the distinct value of a field in the Django models." is select DISTINCT XXX from table_name ... Such a feature. Using values generates Valuesqueryset (a list of n dict), guessing that big data has no additional performance impact, after all, the Queryset series is queried when it is used.
Xxxx.objects.values ("Field_name"). Distinct ()
Xxxx.objects.distinct (). VALUES ("Field_name")
These two sentences generate the same SQL statement, the original address:

About caching:
The queryset is cached, a = A.objects.all (), print [I for I in a]. The first execution of the print queries the database, and the results are saved in the Queryset built-in cache, which is taken from the buffer when the print is executed.
A lot of times you will have to judge if Queryset is empty, you can 1. If Queryset:pass 2.if queryset.count>0:pass 3.if queryset.exists ():p. Three ways to improve performance in turn.
When the queryset is very large, the cache becomes a problem. At this point can be queryset.iterator (), the use of iterators is not much to say, according to the specific needs of the situation.

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: 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.