Django:如何在注释中获取对象而不仅仅是外键?

Gab*_*iMe 7 django

外键上的注释返回外键.如何从查询中获取对象本身?在以下示例中,'the_user'是"投票"模型中的外键:

Vote.objects.values('the_user').annotate(vote_count=Count('the_user')).order_by('-vote_count')
Run Code Online (Sandbox Code Playgroud)

它会回来

[{'the_user': 4, 'vote_count': 12} , {'the_user': 6, 'vote_count': 2}]
Run Code Online (Sandbox Code Playgroud)

但我需要用户自己的对象..不是ids

Dom*_*tos -1

您获得外键而不是用户对象的原因是因为您使用了values(),values 返回一个字典而不是模型对象。使用 select_lated 而不是值,这将解决您的问题。