0

I am looping over a queryset and fetching a one-to-many field. Now, for each call there is a query that is being fired. Django 1.4 has prefetch_related. How can I do something similar with Django 1.3.1 ?

Aman
  • 4,786
  • 1
  • 17
  • 17
  • I've written about various techniques for this on my blog: http://blog.roseman.org.uk/2010/01/11/django-patterns-part-2-efficient-reverse-lookups/ – Daniel Roseman Jul 04 '12 at 14:33

1 Answers1

0

try https://github.com/ionelmc/django-prefetch

sounds good:

Simple and generic model related data prefetch framework for Django solving the "1+N queries" problem that happens when you need related data for your objects. In most of the cases you'll have forward relations (foreign keys to something) and can use select_related to fetch that data on the same query. However, in some cases you cannot design your models that way and need data from reverse relations (models that have foreign keys to your objects). Django 1.4 has prefetch_related for this, however, this framework provides greater flexibility than Django 1.4's prefetch_related queryset method at the cost of writting the mapping and query functions for the data. This has the advantage that you can do things prefetch_related cannot (see the latest_book example bellow).

dgk
  • 330
  • 3
  • 5