[Answer]-Django: extend queryset or exclude from exclude method

1👍

Let’s rewrite

MyModel.objects.filter(var1__lte=10,var2=True).exclude(Q(var4=10) | Q(var5=20))

As

X = Q(var1__lte=10, var2=True) & ~(Q(var4=10) | Q(var5=20))
MyModel.objects.filter(X)

The other query is simply

Y = Q(name__in=must_include_list)

Now what you want is X or Y so

MyModel.objects.filter(X | Y)

Tip: if you rename X and Y to meaningful names, your code will become readable and clean, and less cryptic than reading the nested filter/exclude and logic operators

👤bakkal

Leave a comment