django-Q模块实现查询

django Q模块

from django.db.models import Q


def search(request):
    q = request.GET.get('q')
    if q: # 查询字段不能为None,否者报错
        # title或者content中包含了搜索的关键字
        newses = News.objects.filter(Q(title__icontains=q)|Q(content__icontains=q))  
        context = {
            'newses': newses
        }
    else:
        context = {}
    return render(request,'news/search.html',context=context)

更多的查询方法: http://www.cnblogs.com/tangpg/p/9010610.html

原文地址:https://www.cnblogs.com/tangpg/p/9413010.html