I want to make a query, something like
Model.objects.filter(x=x).filter(y=y).filter(z=z)
… but there are some cases when for example y is None. This literally searches the database for null values in the y column — is there a nifty way to essentially disregard that query parameter if it is none, i.e. return the queryset
Model.objects.filter(x=x).filter(z=z)?
I do not know, if I get your question, but
gives you the queryset, where the
ycolumn is non-null (IS NOT NULL).Here’s the relevant documentation.
EDIT: Check if y is None and build your queryset dynamically:
If there are too many arguments to deal with, you could use something like this; assuming that
x,y,zare stored in a dictionaryyour values: