Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    Post.objects.prefetch_related('author__company')
Often I want to avoid joins on big tables but still need to prefetch related fields on the objects I'm fetching. Example above fetches all the authors for the returned post, and then fetches all their companies. That's two additional fetches I don't have to write.


It's also simple to prefetch in SQL. For example, if you wanted to populate a temp table with the results of a select query in SQL Server, this can be a one line change:

http://stackoverflow.com/a/12323794




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: