I have following Models in my Django DRF app.
class FilterValue(models.Model):
code = models.Charfield(…)
class ProductVariant(models.Model):
filters = models.ManyToManyField("FilterValue", blank=True, …)
class Product(models.Model):
variants = models.ManyToManyField("ProductVariant", blank=True, …)
category = models.ForeignKey("Category", blank=True)
And I’m trying to define function on Category model which will return all objects of FilterValue attached to all ProductVariants of all Products that are assigned in the category.
Since I’ll have loads of different ProductVarints I can’t get away with nested loops since it’d be really slow.
I had multiple attempts to solve it.
I can of course get all Products within a Category using:
products = Product.objects.filter(category=self)
But then I get stuck on the fact that I actually need to filter nested ManyToMany object since I need all ProductVariants of all Products in the QuerySet and then - in the another level I need all ManyToMany FilterValue objects of each ProductVariant.
Thank you.