0

在转换这行 sql 时,我对将 sql 查询转换为 django ORM 函数调用感到困惑

AND (presentations.teaching_guide_text LIKE %s OR presentations.name LIKE %s OR presentations.synopsis LIKE %s OR presentations.tags LIKE %s) AND

我试过这样做,

Presentations.objects.\
                        filter(deleted=0).\
                        filter(published=1).\
                        filter(academic_only=0).\
                        filter(video_type=1).\
                        filter(synopsis__icontains=search_item_array[i]).\
                        filter(tags__icontains=search_item_array[i]).\
                        filter(teaching_guide_text__icontains=search_item_array[i]).\
                        filter(name__icontains=search_item_array[i]).all()

但它不能正常工作。谁能建议我解决这个问题的解决方案

4

1 回答 1

0

应用连续filters有效地链接过滤器AND。你想要的是Q Objects。见下文:

from django.db.models import Q
# breaking them out for readability

q1 = Q(synopsis__icontains=search_item_array[i])
q2 = Q(tags__icontains=search_item_array[i])
q3 = Q(teaching_guide_text__icontains=search_item_array[i])
q4 = Q(name__icontains=search_item_array[i])

Presentation.objects.filter(q1|q2|q3|q4).filter(..).filter(..)

过滤器的第一部分执行OR反对q1,q2,q3,q4,接下来的过滤器将是您的传统ANDs。

于 2013-06-27T06:55:11.120 回答