Python Learning Note-day022-f,q

Source: Internet
Author: User

There's another F and a Q in Django.


How to Import

From Django.db.models import F


About F:

How to use

F more say, he is used to specify the fields in the table,

For example, when the Size field in TABLE01 plus 1,

The statement in SQL is

Update table01 Set size = Size +1

In Django, it's

Models.table01.objects.all (). Update (size = F ("size") +1)

F ("size") represents the size of the current row, which is the function of F.




About Q

For conditional queries

Like some websites provide advanced searches that specify multiple search criteria, you can do so with Q.


How to use:

From Django.db.models import Q



Condition1 = Q ()

Condition2 = Q ()

Condition3 = Q ()


# set Condition1, multiple conditions, or relationships, or you can set other relationships

Condition1.connector = "OR"

Condition1.children.append (("id", 1))

Condition1.children.append (("id", 3))


# set Condition2, multiple conditions, or relationships, or you can set other relationships

Condition2.connector = "OR"

Condition2.children.append (("Name", "Alex"))

Condition2.children.append (("name", "Eric")


#将 Condition1 and Condition2 together and put them in Condition3.

Condition3.add (Condition1, "add")

Condition3.add (Condition2, "add")


# Make a conditional query.

Models. TableName.objects.filter (Condition3)


This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1766023

Python Learning Note-day022-f,q

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.