18. query operation

 

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==xxxfact is equivalent to field=xxx, so we directly use the field=xxxsolution of it, and because in most cases exactand iexactit is equivalent, so we use directly after field=xxxit

3 QuerySet.query: querycan be used to view the OMRquery is translated into the final SQLstatement however. queryCan only be used QuerySeton the object, it can not be used in common ORM模型on so if your query is passed. getTo get the data, you can not use querybecause the getreturn is to meet the conditions of ORMthe model, rather than the QuerySettype, if you are by filterother returns and other QuerySetmethods of inquiry, then you can usequery

  1. contains: Use case-sensitive judgment, a string of whether the query will be used in the designated field size sensitive, so be translated into SQLthe statement when use like binary, and like binaryis using case-sensitive.
  2. icontains: Ignore case, to determine whether a string in a specified field in this query is translated into. SQLWhen using like, and likethe MySQLlevel is not case-sensitive.

Note: containsand icontains: being translated into the SQLuse of time is the %hello%, as long as there is a whole string helloit can be found and. iexactNo %, it means that only time will be exactly equal to the match.

  1. 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 write models_set, direct use of lowercase name of the model on it, such as by classification to find the article, then through article__id__init, rather than written article_set__id__inform if the definition ForeignKeyis passed time related_query_name=articlesto develop a double underscore name back reference, then when the double-underlined reverse lookup will use articles__id__inthe 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'
    
    


    Because categoryof ForeignKeyspecified related_query_name, so you can no longer use articleto perform a reverse lookup. By this time you need articlesto 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 written article__inon it
    : Note ForeignKeyparameters: related_name: it is time to reverse lookup the object model used, and related_query_nameit's time to double underscore reverse lookup method of use.

    inNot only can develop a list / tuple may also be QuerySetsuch 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)
    
    
  2. 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)
    
  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")
    
  4. About the time of the query:

    • range: You can specify a time period. And time should be marked as awaretime, 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 MySQLadded 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 rangeconditions. 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))
      

Guess you like

Origin www.cnblogs.com/ys-python/p/11266204.html
Recommended