1π
β
I donβt understand the point of the ValuesQuerySet. You loop through it, but you then use the ID to do another db lookup in each row, to query the original item! Why do you do that? Why get the values at all? Why not just:
facility_list = Facility.objects.all().select_related('current_address__region')
rows = []
for facility in facility_list:
row = {'id': facility.id, 'name': facility.name, 'type': facility.type}
row['name'] = "<a href=\"%s/detail\">%s</a>" % (row['id'], row['name'])
...etc...
rows.append(row)
Now (ignoring the subsequent lookups inside the methods) that is one single query, rather than one query per row.
π€Daniel Roseman
Source:stackexchange.com