Django Q Query

Source: Internet
Author: User

Q Query--complex queries on objects.
F Query-An operation that specifically takes a column value from an object.

Q Query:

1. Q object (DJANGO.DB.MODELS.Q) can encapsulate keyword parameters to better apply multiple queries, for example:

From django.db.models import qfrom login.models import new #假设在我的login app models.py There is a new data class New=new.objects.filter (Q ( Question__startswith= ' what '))

2, can be combined with the &,| operator, when an operator is used for two Q object, it produces a new Q object.

Q (question__startswith= ' Who ') | Q (question__startswith= ' what ')

3, Q object can be used in the ~ operator in front of the negative, can also allow the combination of negative and non-negative form.

Q (question__startswith= ' Who ') | ~q (pub_date__year=2005)

4. Application scope

Poll.objects.get (q (question__startswith= ' who '), Q (Pub_date=date (2005, 5, 2)) | Q (Pub_date=date (2005, 5, 6))) is equivalent to the SELECT * from polls WHERE question like ' who% ' and (pub_date = ' 2005-05-02 ' OR pub_date = ') 2005-05-06 ')

5, Q object can be used with the keyword parameter query, but be sure to put the Q object in front of the keyword parameter query.

Correct: Poll.objects.get (Q (Pub_date=date (2005, 5, 2)) | Q (Pub_date=date (2005, 5, 6)), question__startswith= ' who ') Error: Poll.objects.get (question__startswith= ' who ', Q (pub_ Date=date (2005, 5, 2)) | Q (Pub_date=date (2005, 5, 6)))

Django 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.