Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 413
John Waller Avatar asked Dec 14 '22 08:12

John Waller


2 Answers

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

like image 132
Sayse Avatar answered Jan 06 '23 12:01

Sayse


As I commented earlier, if you just need all the colors of an Entry together, you can select all the Color objects and order them on entry:

colors = Color.objects.order_by('entry')

Now, you can loop over these objects and print them the way you want:

for color in colors:
    print(color.entry.user, color.color)

# john, blue
# john, orange
# bob, green

Or, you can extract this information as values_list

color_entries = list(colors.values_list('entry__user', 'color'))
# [('john', 'blue'), ('john', 'orange'), ('bob', 'green'), ...]
like image 37
AKS Avatar answered Jan 06 '23 13:01

AKS