django - Trying to get Count('foo__bar') inside annotate() -


im trying following line works:

artists = models.artists.objects.all().annotate(tot_votes=count('votes__work')).order_by('-tot_votes') 

(i.e. want annotate count of votes corresponding every artist.)

but whenever above line executed fielderror error cannot resolve keyword 'votes' field.

where

class votes(models.model):     work = models.onetoonefield(works, models.do_nothing, db_column='work')     user = models.onetoonefield(authuser, models.do_nothing, db_column='user') 

and

class works(models.model):     artist = models.foreignkey(artists, models.do_nothing, db_column='artist')     # other irrelevant fields 

or relation between tables (votes --> works --> artists)

i think you've got relationship wrong way round, haven't you? artist doesn't have kind of direct relationship votes, works. annotate call should count('work__votes').

(also note normal naming convention django use singular names: vote, work, artist.)


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -