ionelmc/django-prefetch — Issue #2: Make it possible to pass parameters to .prefetch()
Author: mkai
Created: 2012-02-20 01:46:49+00:00
State: open

I'd like to make it possible to pass parameters to the ``prefetch`` method of the ``PrefetchManager``.

The current API is this:

```python
videos = Video.objects.all().prefetch('tags', 'comment')[:10]
```

But what if I wanted to decide on a per-use basis that I wanted my ``Prefetcher`` to prefetch only the tags from the last two days? It would be nice if I could include this as a parameter in ``prefetch`` instead of defining a different ``Prefetcher`` for each use-case. This is what an API could look like:

```python
from prefetch import PrefetcherOptions as P
videos = Video.objects.all().prefetch(P('tags', max_age=14, order_by='-name'), 'comments')[:10]
```

Now, If I also wanted to prefetch only those comments that are longer than 200 chars (for example), I would call it like this:

```python
videos = Video.objects.all().prefetch(P('tags', max_age=14, order_by='-name'), P('comments', min_chars=200))[:10]
```

If ticket #1 can be solved in any way, we could also limit the number of fetched objects per parent in the same way:

```python
pizzas = Pizza.objects.all().prefetch(P('toppings', per_pizza=5))[:10]
```

What do you think?

Also, is there a reason (other than for API convenience) that the ``filter``, ``reverse_mapper`` and ``decorator`` methods need to be defined as static when used in a Prefetcher subclass? It makes dynamic behaviour like this a bit hard because there is no reference to neither the Prefetcher nor the PrefetchManager instance.