0

I have a queryset with many attributes and potentially 1000s of rows. I use set comprehension to identify unique attributes within the queryset in order to create dropdown filters that help users filter the objects in a table. Currently, I have created a set comprehension for each filtered attribute.

Is there a more efficient way to obtain these unique attribute values?

models.py

class Foo(models.Model):
    origin = models.ForeignKey('Origin', blank=False, null=False, db_index=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=50, null=True, blank=True)
    group = models.CharField(max_length=50, null=True, blank=True)
    team = models.CharField(max_length=50, null=True, blank=True)

class Origin(models.Model):
    name = models.CharField(max_length=50, null=True, blank=True)

views.py

qs = Foo.objects.filter(criteria=unique_to_each_user).select_related('origin')
filter_name = sorted({q.name for q in qs if q.name})
filter_group = sorted({q.group for q in qs if q.group})
filter_origin = sorted({q.origin.name for q in qs if q.origin})

For a few attributes, I also create a tuple that contains total count along with each unique attribute:

l_team = lambda x: x.team
team_totals = {total: sum(1 for q in qs) for total, qs in groupby(sorted(qs, key=l_team), l_team)}
filter_team = sorted({(q.team, team_totals.get(q.team)) for q in qs if q.team})

I use qs for other purposes, and was attempting to avoid several trips to the DB by performing the set comprehension actions on qs, as opposed to using a technique at the DB level, such as distinct - link. However, these set comprehensions are hurting performance.

Perhaps I am misguided in minimizing DB queries above all else? Is there a better way to get unique values for several attributes in a queryset?

wraasch
  • 405
  • 3
  • 13

0 Answers0