开发者

Django orm query help

开发者 https://www.devze.com 2023-02-07 01:51 出处:网络
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?

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('开发者_C百科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.


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 ?...

0

精彩评论

暂无评论...
验证码 换一张
取 消