[Django]-Django: SQL query with distinct on model function

3👍

You’er looking for values and annotate
https://docs.djangoproject.com/en/dev/topics/db/aggregation/#order-of-annotate-and-values-clauses

Which will give you unique values for a values call.

import itertools

class_a_names = user.project_set.values_list('class_a__teacher_name', flat=True).annotate()
class_b_names = user.project_set.values_list('class_b__teacher_name', flat=True).annotate()
unique_teachers = set(itertools.chain(class_a_names, class_b_names))

Leave a comment