Django 【models】

query_set()

  • 根据字典查询数据库,而不是models.phones.object.filter(phone="xxx")
    # 一种根据多个参数查询数据库的方法
    # 理想状态下,我可以传入多个参数,直接查询
    # 原文:https://docs.djangoproject.com/en/3.0/ref/request-response/#querydict-objects
    from django.http import QueryDict
    
    q = QueryDict('phone=15111111111')   #可以直接使用__gte、__lte等...
    models.Phones.object.filter(**q.dict())
    
    # 由此衍生出Q查询
    from django.db.models import Q
    q = Q(**{"phone":"15111111111"})
    models.Phones.object.filter(q)
    
    #### 其实最直接的是:
    q = {"phone":"15111111111","start__gte":2}
    models.Phones.object.filter(**q)
    
posted @ 2021-08-30 14:20  lisicn  阅读(23)  评论(0编辑  收藏  举报