I've tried prefetch_related() in django 1.4 from trunk and can't make it to prefetch reverse lookup.
My simplified models (each book has many prices):
class Book(models.Model): # some fields class Price(models.Model): book = models.ForeignKey(Book)
My view's query:
books = Book.objects.prefetch_related('price')
Then, I got the AttributeError message:
AttributeError: Cannot find 'price' on Book object, 'price' is an invalid parameter to prefetch_related()
How to make it work? Thanks.
In Django, select_related and prefetch_related are designed to stop the deluge of database queries that are caused by accessing related objects. In this article, we will see how it reduces the number of queries and make the program much faster.
The difference is that select_related does an SQL join and therefore gets the results back as part of the table from the SQL server. prefetch_related on the other hand executes another query and therefore reduces the redundant columns in the original object ( ModelA in the above example).
select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. For this reason, select_related gets the related objects in the same database query.
Thats' where related name or the reverse relationship comes in. Django, by defaults gives you a default related_name which is the ModelName (in lowercase) followed by _set - In this case, It would be profile_set , so group. profile_set . However, you can override it by specifying a related_name in the ForeignKey field.
Define a related name:
class Price(models.Model): book = models.ForeignKey(Book, related_name='prices')
and then use it:
books = Book.objects.prefetch_related('prices')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With