[Answered ]-How can I remove duplicate objects in an order_by query?

2👍

If your database backend were PostgreSQL, you could do it with a queryset:

a = group.objects.order_by('groupname').distinct('groupname')

Unfortunately you are using SQLite, so you would preferably do it in python :

a = group.objects.order_by('groupname')
groupnames = set()
b = []
for item in a:
  if a.groupname not in groupnames:
    b.append(a)
    groupnames.add(a.groupname)
a = b

Leave a comment