how to efficiently use _set.all() in django?

John Waller picture John Waller · Mar 9, 2016 · Viewed 18.9k times · Source

Is there a way in django to do the following more efficiently when the number Entry objects is greater than 5000 entries?

models.py

class Entry(models.Model):
    user = models.TextField(db_column='User', blank=True)
    date = models.DateTimeField(blank=True)

class Color(models.Model):
    color = models.TextField(blank=True)
    entry = models.ForeignKey(Entry)

And let's say that I wanted to get all the colors for each of these entries...

entrys = Entry.objects.all()

for e in entrys:
    print e.color_set.all()

I want to be able to relate each object to a specific entry. For example, in a csv table like this.

user, color
john, blue
john, orange
bob, green
bob, red
bob, purple

It takes several seconds to look through all of my entries. Is there a better way?

Answer

Sayse picture Sayse · Mar 9, 2016

You should use prefetch_related

entrys = Entry.objects.all().prefetch_related('color_set')

for e in entrys:
    print e.color_set.all()

Instead of doing n, queries it will do 2, one for the entries, and one for the foreign key lookups