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