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