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?
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
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'), ...]
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