Django 模糊查询

Posted 蜗牛赛跑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘)

以上是关于Django 模糊查询的主要内容,如果未能解决你的问题,请参考以下文章

Django 模糊查询

Django模糊查询

Django模糊查询

django怎么模糊匹配json中的数据?

django全文检索

django之搜索引擎功能实现