Django 模糊查询

 

官方文档地址

https://docs.djangoproject.com/en/1.8/topics/db/queries/#complex-lookups-with-q-objects

 

F查询

 

contains查询

Entry.objects.get(headline__contains='Lennon')
Roughly translates to this SQL:
SELECT ... WHERE headline LIKE '%Lennon%';

icontains查询

q.exclude(body_text__icontains="food")

exact   查询

Entry.objects.get(headline__exact="Man bites dog")

iexact查询
Blog.objects.get(name__iexact="beatles blog")

Q查询:

from django.db.models import Q
Q(question__startswith='What')


Q(question__startswith='Who') | Q(question__startswith='What')
This is equivalent to the following SQL WHERE clause:

WHERE question LIKE 'Who%' OR question LIKE 'What%'


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


Poll.objects.get(
    Q(question__startswith='Who'),
    Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))
)
... roughly translates into the SQL:

SELECT * from polls WHERE question LIKE 'Who%'
    AND (pub_date = '2005-05-02' OR pub_date = '2005-05-06')
posted @ 2016-06-30 15:37  schangech  阅读(8606)  评论(0编辑  收藏  举报