I want to query a database in a way such that rather than just ordering by some field, I get a separate QuerySet (or dictionary, list, whatever) for each unique value of that field. Hopefully the below example will help:
Suppose a model like
Class Person(models.Model):
first_name = models.CharField()
last_name = models.CharField
Calling Person.objects.all().order_by(‘last_name’) gives me a single long QuerySet. I want instead to have a separate list for each unique last_name. So one list for every Person with last_name=”Smith” and another list for every Person with last_name=”Nguyen” etc.
Obviously I can’t know ahead of time what last_names will be in the database, nor how many people will share a common last_name. Is there any fast, efficient, or automatic way to do this in django or do I just need to process the data myself after getting the one large queryset back?
you can get all the unique lastnames:
Daniel-Roseman is right it is pretty inefficient so heres a tweak version …
note that
_ = ...is so we don’t print all theNoneon the terminal 😉