python - Count and Max after values() method on Django query -


i have 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 following:

  1. filter on game, step , type
  2. find player/players how has/have obtained highest number of actions

to 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 select max 

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

cannot compute max('count'): 'count' aggregate 

i know similar has been answered django values() , aggregate() bit tricky me. right way this?

you can highest count using django's .latest() method. though documented dates, works on strings , integers.

this should action highest to_player count:

# combined filters, no need separate 2 steps v = action.objects.filter(game=1, step=2, type=3) v = v.annotate(count=count('to_player')) v = v.latest('count') # return action highest count 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -