ionelmc/django-prefetch — Issue #1: Ideas on how to limit the number of related objects per parent object
Author: mkai
Created: 2012-02-20 01:21:54+00:00
State: open

First, I'd like to say a big Thank You for publishing this package. It provides the most elegant solution to some long-standing performance-related problems I'm having in my project!

Do you have an idea on how the number of related objects that are prefetched can be limited per parent object? For example, if I had the following model (it's the one used in Django's ``prefetch_related`` doc):

```python
class Topping(models.Model):
name = models.CharField(max_length=30)

class Pizza(models.Model):
name = models.CharField(max_length=50)
toppings = models.ManyToManyField(Topping)

def __unicode__(self):
return u"%s (%s)" % (self.name, u", ".join([topping.name
for topping in self.toppings.all()]))
```

If I wanted to display a list of pizzas, each of them together with their toppings, ``django-prefetch`` provides a nice solution to exclude, for example, the tomato sauce and the cheese ;). But how could a solution be implemented to prefetch only 5 toppings per pizza? I've seen some SQL examples involving JOINs and subqueries, and it seems like a non-trivial problem to solve.

Do you happen to have an idea on how to implement that? I'm worried that, in a situation where we had a huge number of toppings in the database, memory usage would be very high because as with ``prefetch_related``, all toppings for each pizza would be prefetched and read into memory, even though only 5 are actually being used.