[Fixed]-Django filter a ForeignKey field when it is null


After some experiments it seems that .exclude(for_field__bool=False) will contain also for_field__isnull=True entries and will not raise any exceptions. You can be sure by executing .exclude(for_field__bool=False).filter(for_field__isnull=True) and see some results also.

And honestly I don’t know which option is faster, but IMO your variant with two Q objects much more readable because it shows logic you’re really want. So I actually suggest you to stick with it.


I’m pretty sure, that your option is the shortest possible (correct me if I’m wrong). That is because you can’t do OR queries without Q objects.

Leave a comment