How to dynamically compose an OR query filter in Django?

后端 未结 14 1365
清歌不尽
清歌不尽 2021-01-22 05:24

From an example you can see a multiple OR query filter:

Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))

For example, this results in:

14条回答
  •  旧时难觅i
    2021-01-22 06:01

    easy..
    from django.db.models import Q import you model args = (Q(visibility=1)|(Q(visibility=0)&Q(user=self.user))) #Tuple parameters={} #dic order = 'create_at' limit = 10

    Models.objects.filter(*args,**parameters).order_by(order)[:limit]
    

提交回复
热议问题