Combined condition query of Python database query-F & amp; Q query, and python database query-f

Source: Internet
Author: User

Combined condition query of Python database query-F & Q query, python database query-f

F query (obtain the field value)

We know about the query that there are filters (), values (), get (), exclude (). If it is an aggregate group, aggregate and annotate are also used, and there are even omnipotent double underscores, however, if there is such a requirement that the value of the aa field in Table a is greater than the value of the bb field in Table B, what should we do? Django provides an F expression to support this operation?

First, import the module:

from django.db.models import F
a.objects.filter(aa__gt=F('bb'))

Let's take another simple example. Modify the value in the table and add 20 to the value of the price field in table.

a.objects.all().update(price=F('price')+20)

This requirement cannot be implemented without F objects

 

Django supports addition, subtraction, multiplication, division, modulus, and Power Calculation of F () objects.

The F () brackets also support double-underline table join queries. The results returned by F () are not necessarily numbers or strings. For example

from django.db.models import F
a.objects.filter(name=F('b__name'))

 

 

Q queries are widely used compared to F queries. Even if you only need one filter and Q for queries, this is enough.

Import required

from django.db.models import Q

Keywords in the filter () method are searched based on the relationship between 'and'. If you need the relationship between 'or', you need to use Q.

The Q object is used to encapsulate a set of keyword parameters, which can be used as filter () parameters.

Focus:Q objects can be used | or & connected to a new Q object ~ Reversed symbol

Note:: Q objects can be used as filter parameters together with other keyword parameters.But the Q object must be placed in one location.

Example: query data with an id greater than 4, starting with 'zhang', or with a price greater than 10000

A. objects. filter (Q (name _ startwith = 'zhang '), Q (id _ gt = 4) | Q (price _ gt = 1000 ))

 

Extension:

Because the filter receives a keyword parameter, in actual project applications, we can process this keyword parameter separately, for example, writing a dictionary dict, and thenFilter (** dict)You can apply the parameters we wrote in the dictionary.

If it is a Q object, you don't need **

If the Q object is used as the dictionary element, if it is a 'or' relationship, you need to write it like this

con = Q()con.connector = 'OR'key = ......val = .......con.children.append((key,val))dict = {‘key2’:‘val2’}

data_list = a.objects.filter(**dict).filter(con)

 

Related Article

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.