Count and Max after values() method on Django query

会有一股神秘感。 提交于 2019-12-21 23:48:55

问题


I have this Django model:

class Action(models.Model):
    id = models.AutoField(primary_key=True)
    game = models.ForeignKey(Game)
    step = models.ForeignKey(Step)
    from_player = models.ForeignKey(Player)
    to_player = models.ForeignKey(Player)
    type = models.ForeignKey(ActionType)
    timestamp = models.DateTimeField(default=timezone.now)

I want to do the following:

  1. Filter on game, step and type
  2. Find the player/players how has/have obtained the highest number of actions

to do so I tried:

v = Action.objects.filter(game=1, step=2)
v = v.filter(type=3)
v = v.values('to_player').order_by().annotate(count=Count('to_player'))
v = v.annotate(max=Max('count')).filter(count=F('max')) #try to select the max

but last line gives me (because line 3 returns a list of dictionaries):

Cannot compute Max('count'): 'count' is an aggregate

I know that probably something similar has already been answered but Django values() and aggregate() are a bit tricky to me. Which is the right way to do this?


回答1:


You can get the highest count using Django's .latest() method. Though documented for dates, it also works on strings and integers.

This should get you the Action with the highest to_player count:

# combined the filters, no need to separate into two steps
v = Action.objects.filter(game=1, step=2, type=3)
v = v.annotate(count=Count('to_player'))
v = v.latest('count') # will return Action with the highest count



回答2:


The error gives the clue, you should use aggregate instead of annotate for max:

v = Action.objects.filter(game=1, step=2)
v = v.filter(type=3)
v = v.values('to_player').order_by().annotate(count=Count('to_player'))
v = v.aggregate(max=Max('count')).filter(count=F('max')) #try to select the max

I'm not sure if the last filter would work but worth to try.



来源:https://stackoverflow.com/questions/23279393/count-and-max-after-values-method-on-django-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!