Following problem:
I have product groups containing products. These products can be visible in the frontend or not. I determine their visibility with the method frontend()
(which contains a filter) like so:
product_groups.first().products.frontend()
Now I want to determine if I want to put a link for the product group on the homepage only if there are four or more products in it.
With annotations I would do:
product_groups.annotate(num_products=Count('products')).filter(num_products__gte=4)
But this gives me of cause the count of all products and not the count of products visible in the frontend.
So how do I put the additional filter frontend()
into my query? To be clear, I want the Count()
not on 'products'
but on products.frontend()
.
Edit:
This is not a duplicate of the suggested question. If the filter function frontend()
was simple enough to pull out the filter and stick it in the aggregate function, the suggested question would answer my problem.
My frontend()
function is quite complicated and an aggregate of multiple other filter functions. So I would really like to use the frontend()
function.
Edit:
This needs to work in Django 1.8.