Always True Q object Always True Q object django django

Always True Q object


Here's a hacky way to get an always true Q object:

always_true = ~Q(pk=None)

This depends on the fact that the primary key cannot be null.


as Alasdair answered in comment:

Obj.filter(    Q(some_f1=some_v1) if some else Q(),     f1=v1,    f2=v2,    f3=v3,    f4=v4,    ...)


Try this;

conditions = {'f1':f1,'f2':f2, 'f3':f3}if some:    conditions['some_f1'] = some_v1Obj.objects.filter(**conditions)