基于字符串的存在过滤Django Queryset

时间:2018-11-21 03:21:01

标签: python django django-views

我有一个模型,可在views.py中呈现一个json文件:

 def bill_sum(request):
  data = Summary.objects.values('title','summary','summary_text')
  return render(request,'billsummary.html',context={'data':data})

是否可以通过查找要从urls.py中传入的特定字符串来过滤summary_text对象查询?

类似于

from django.db.models import Q Summary.objects.values('title;'summary',Q('summary_text'__contains=someword),...)

1 个答案:

答案 0 :(得分:3)

确定有可能。尝试这样:

def some_view(request);
    somewords  = request.GET.get('somewords')
    data = Summary.objects.filter(summary_text__icontains=somewords).values('title','summary','summary_text')  # icontains has been used here instead of contains to ignore uppercase/lowercase issues