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')


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM