Reputation: 159
I am triying to query my product model like this:
So, my products can be sold by an user that has a store or an user that not has a store; what I want to do is to make query adding these extra parameters when "has_store" condition beign True like this.
store_query = ({"seller__user_store__is_active": True,
"seller__user_store__is_visible": True}
if F("seller__user_store__isnull=False") else {})
And then add that query to my filtering sentence:
Product.objects.filter(Q(is_new=True) & Q(is_active=True), **store_query)
My product model also has is_new, and is_active and other parameters.
So, expected behaviour is something like add Q(seller__user_store__is_visible=True)
and Q(seller__user_store__is_active=True)
if product seller has a related store
I hope have been clear, thanks you a lot
Upvotes: 1
Views: 1459
Reputation: 477309
I think you make things too complicated. The stored_query
is just equivalent to:
Q(seller__user_store=None) | Q(seller__user_store__is_active=True, seller__user_store__is_visible=True)
Indeed, in case the seller__user_store=None
, it does not have to look to the conditions of the seller, otherwise it will. We can thus implement this as:
Product.objects.filter(
Q(seller__user_store=None) | Q(seller__user_store__is_active=True, seller__user_store__is_visible=True),
is_new=True, is_active=True
)
Upvotes: 1