参考:http://stackoverflow.com/questions/6567831/how-to-perform-or-condition-in-django-queryset

django自带的orm,虽然给我们写代码带来了方便,但是由于本身的一些限制,有些复杂的sql查询语句没办法时间,今天就说一下django  orm中的  或者 查询。

SELECT * from user where income >= 5000 or income is NULL.

  上面的sql语句我们查询收入大于5000或者收入 为空的记录。下面介绍两种方法实现  or  查询

第一种方法:使用 Q查询

from django.db.models import Q
User.objects.filter(Q(income__gte=5000) | Q(income__isnull=True))

 

第二种方法:使用 管道符号  | ,

combined_queryset = User.objects.filter(income__gte=5000) | User.objects.filter(income__isnull=True)
ordered_queryset = combined_queryset.order_by('-income')                                                    

相关文章:

  • 2022-01-18
  • 2021-12-31
  • 2022-02-16
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
猜你喜欢
  • 2022-03-04
  • 2021-05-26
  • 2022-12-23
  • 2022-03-08
  • 2021-05-30
  • 2021-07-13
相关资源
相似解决方案