Query-f&q query for the combination condition of Python database query

Source: Internet
Author: User

F Query (take the value of the field)

About the query we know that there is filter (), values (), get (), exclude (), and if it is an aggregation grouping, it will also use aggregate and annotate, and even a universal double underline, but if there is such a requirement, The value of the AA field in query a table is greater than the value of the BB field in table B, what should be done, Django provides an F-expression to support this operation

The module should be imported first:

From Django.db.models import F
A.objects.filter (aa__gt=f (' BB '))

To give a simple example, modify the values in the table to the value of the Price field in each table A, plus 20

A.objects.all (). Update (Price=f ('price') +20)

If there is no F object, this requirement cannot be achieved.

Django supports the addition, subtraction, multiplication, addition, modulo, power calculation of f () objects

The f () brackets also support double underscores for a table query, and F () returns a result that is not necessarily a number or a string, such as

From Django.db.models import F
A.objects.filter (name=f ('b__name'))

Q Query, compared to F query, Q combination Query application is very extensive, even your query only need a filter and q, enough

Need to import

 from Import Q

The keyword in the filter () method is searched by the relationship between ' and ', and if you need ' or ', you have to use Q.

The Q object is used to encapsulate a set of keyword parameters that can act as parameters to the filter ()

The point is that the Q object can be used | or & connected to a new Q object, also supports using the ~ symbol to reverse

Note : The Q object can be used with other keyword parameters as the filter parameter , but the Q object must be placed in one position

Example: Querying a table with IDs greater than 4, beginning with ' Zhang ', or data with a price greater than 10000

A.objects.filter (q (name__startwith=' Zhang '), Q (id__gt=4)) | Q (price__gt=1000))

Extended:

Because the filter receives the parameter is the keyword parameter, so in the actual project application, we can deal with this keyword parameter separately, for example writes a dictionary dict, then the filter (**dict) can apply the parameter which we write in the dictionary

If it's a Q object, you don't have to.

When using the Q object as the element of the dictionary, if it is a ' or ' relationship, it needs to be written

con ='OR'== .... Con.children.append ((key,val))dict = {' Key2 ': ' Val2 '}

Data_list = A.objects.filter (**dict). Filter (Con)

Python database query combination condition Query-f&q query

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.