Query operation
Double offline magic method
1 exact
: In the bottom will be translated =
2 iexact
: it can be translated into the underlying like
* = like and: are equivalent in most cases, only a few cases are not equivalent
* exact and iexact: the difference between them is actually like and = difference because the exact will be translated into =, which will be translated into iexact like.
* because the field__exact==xxx
fact is equivalent to field=xxx
, so we directly use the field=xxx
solution of it, and because in most cases exact
and iexact
it is equivalent, so we use directly after field=xxx
it
3 QuerySet.query
: query
can be used to view the OMR
query is translated into the final SQL
statement however. query
Can only be used QuerySet
on the object, it can not be used in common ORM模型
on so if your query is passed. get
To get the data, you can not use query
because the get
return is to meet the conditions of ORM
the model, rather than the QuerySet
type, if you are by filter
other returns and other QuerySet
methods of inquiry, then you can usequery
contains
: Use case-sensitive judgment, a string of whether the query will be used in the designated field size sensitive, so be translated intoSQL
the statement when uselike binary
, andlike binary
is using case-sensitive.icontains
: Ignore case, to determine whether a string in a specified field in this query is translated into.SQL
When usinglike
, andlike
theMySQL
level is not case-sensitive.
Note: contains
and icontains
: being translated into the SQL
use of time is the %hello%
, as long as there is a whole string hello
it can be found and. iexact
No %
, it means that only time will be exactly equal to the match.
-
in
: Can directly specify the value of a field in a set is in the following sample code:articles = Article.objects.filter(id__in=[1,2,3])
It is also possible through other fields of the table to determine whether a certain set of sample code is as follows:categories = Category.objects.filter(article__id__in=[1,2,3])
To determine if the field associated with the table, it is by__
connecting And when doing back-references, no need to writemodels_set
, direct use of lowercase name of the model on it, such as by classification to find the article, then througharticle__id__in
it, rather than writtenarticle_set__id__in
form if the definitionForeignKey
is passed timerelated_query_name=articles
to develop a double underscore name back reference, then when the double-underlined reverse lookup will usearticles__id__in
the sample code as follows:class Category(models.Model): name = models.CharField(max_length=100) class Meta: db_table = 'category' class Article(models.Model): title = models.CharField(max_length=200) content = models.TextField() cateogry = models.ForeignKey("Category",on_delete=models.CASCADE,null=True,related_query_name='articles') class Meta: db_table = 'article'
Becausecategory
ofForeignKey
specifiedrelated_query_name
, so you can no longer usearticle
to perform a reverse lookup. By this time you needarticles
to perform reverse lookups.
And if the time to do a reverse lookup, if the query field is the primary key of the model, you can omit this field, directly writtenarticle__in
on it
: NoteForeignKey
parameters:related_name
: it is time to reverse lookup the object model used, andrelated_query_name
it's time to double underscore reverse lookup method of use.in
Not only can develop a list / tuple may also beQuerySet
such as to query "in the title of the article has all the classified hello" then can be achieved through the following code:articles = Article.objects.filter(title__icontains='hello') categories = Category.objects.filter(articles__in=articles) for cateogry in categories: print(cateogry)
-
gt / gte / lt / lte: it represents greater than / equal to greater than / less than / less than equal conditions.
gt
: Greater Within last;lt
: Lower Within last;gte
: Within last equal Greater;lte
: Lower Within last equal the following sample code:articles = Article.objects.filter(id__lte=3)
-
startswith, istartswith, endswith, iendswith: expressed as a start value, case-insensitive starts with a value ending with a value case-insensitive ends with a certain value. Sample code is as follows:
articles = Article.objects.filter(title__endswith="hello")
-
About the time of the query:
-
range: You can specify a time period. And time should be marked as
aware
time, or django will report a warning. Sample code is as follows:start_time = make_aware(datetime(year=2018,month=4,day=4,hour=17,minute=0,second=0)) end_time = make_aware(datetime(year=2018,month=4,day=4,hour=18,minute=0,second=0)) articles = Article.objects.filter(create_time__range=(start_time,end_time)) print(articles.query) print(articles)
-
date: a date to filter. If you want to use this filter condition, the premise must be
MySQL
added those good time zone file. How do you add it? Reference lesson plans. Sample code is as follows:articles = Article.objects.filter(create_time__date=datetime(year=2018,month=4,day=4))
- year / month / day: it stands for search according to year / month / day. Sample code is as follows:
articles = Article.objects.filter(create_time__year__gte=2018)
- week_day: According week to find it. 1 for Sunday, 7 for Saturday, 2-6 represents Monday through Friday. For example, to find all articles Wednesday, then you can be achieved by the following code:
articles = Article.objects.filter(create_time__week_day=4)
- time: to find it based on the TD seconds. If you want specific to the second, it is generally more difficult to match, you can use the range of ways to find out. Interval use
range
conditions. For example, you want to get the article between 17:00 / 10 min / 27-28 seconds, can be achieved by the following code:
start_time = time(hour=17,minute=10,second=27) end_time = time(hour=17,minute=10,second=28) articles = Article.objects.filter(create_time__time__range=(start_time,end_time))
-