How to perform OR condition in django queryset?

前端 未结 4 1801
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 07:39

I want to write a Django query equivalent to this SQL query:

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

How to construct

4条回答
  •  心在旅途
    2020-12-07 08:03

    Just adding this for multiple filters attaching to Q object, if someone might be looking to it. If a Q object is provided, it must precede the definition of any keyword arguments. Otherwise its an invalid query. You should be careful when doing it.

    an example would be

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

    Here the OR condition and a filter with category of income is taken into account

提交回复
热议问题