美文网首页
Django中ORM之或语句查询

Django中ORM之或语句查询

作者: LeslieLiang | 来源:发表于2019-05-28 15:34 被阅读0次

    比如数据库表中有显示器1和显示器2,那么如何在django中模糊查询出显示器1和显示器2呢

    首先导入模块

    from django.db.models import Q
    
    class GetDisplay(View):
    
    class GetDisplay(View):
        def post(self, request):
            display = request.POST.get('display')
            obj = models.Display.object.filter(Q(display1__icontains = display) | Q(display2__icontains = display))
            return render('index.html', {'displayList' : obj})
    

    __icontains:表示不区分大小写模糊查询
    上面代码的重点是模块Q和查询语句中的|

    相关文章

      网友评论

          本文标题:Django中ORM之或语句查询

          本文链接:https://www.haomeiwen.com/subject/hysdtctx.html