user469652
user469652

Reputation: 51221

Django orm query help

Employee class has id, first_name, last_name, and a lot more fields.

I want to select all the employees that do not have the same last_name, It's something like distinct in SQL, how to do that?

employees = Employee.objects.value('last_name').distinct() will only include the last_name, so I cannot find id.

If I do employees = Employee.objects.value('id', 'last_name').distinct() the results.count() looks different.

Upvotes: 0

Views: 64

Answers (1)

Dominique Guardiola
Dominique Guardiola

Reputation: 3431

That's just because you may have employees with the same last name.
When you add 'id' in the query, you ask for all distincts rows based on the two criterias, and you always have distinct id.
Am I clear ?...

Upvotes: 1

Related Questions