Hi I have a Product model that holds info common to all product types. Product has a m2m categories field. SimpleProduct and BundleProduct extends Product (one2one relation). BundleProduct have a m2m products field (only SimpleProducts allowed).
class Product(models.Model): > name=models.CharField() > description=models.CharField() > categories = models.ManyToManyField(Category) > > class SimpleProduct(Product): > some_field=models.CharField() > > class BundleProduct(Product): > products = models.ManyToManyField(Product) > When showing a list of products I'm making the following queries: > products = Product.objects.filter(....) > for p in products: > for category in p.categories.all(): > ... > if hasattr(p, 'bundleproduct'): > for child in p.products.all(): > for category in child.categories.all(): > This could end with a huge number of queries. I can improve it with: > Product.objects.filter(...).select_related('simpleproduct', > 'bundleproduct') > I can further improve by using the select_reverse app for the m2m categories field. That's a big improvement but it can still end with a large number of queries because each BundleProduct can have several products. There are other product types like ConfigurableProduct. So a BundleProduct can have a ConfigurableProduct child that has several SimpleProduct children Is it possible to make a custom query that will fetch everything with a single query and put it in a queryset so I can iterate it and work with it as usual? The docs about raw queries show simple use case. Is it possible to fetch deep nested objects like products[1].categories.all()[2] without making additional queries? Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.