python - Stumped by model manager's errant behavior when summing up/down votes and displaying an ordered list -
this 1 might tricky.
i have basic web app users post interesting urls, , upvote/downvote best/worst ones. i'm using python 2.7, django 1.5. problem statement in bold @ end.
in models.py, have following lines (take special note of lines marked x):
class link(models.model): submitter = models.foreignkey(user) url = models.urlfield("url", max_length=250, blank=true) x with_votes = linkvotecountmanager() def __unicode__(self): return self.url class linkvotecountmanager(models.manager): def get_query_set(self): x return super(linkvotecountmanager, self).get_query_set().annotate(votes=sum('vote__value')).order_by('-votes') class vote(models.model): voter = models.foreignkey(user) link = models.foreignkey(link) x value = models.integerfield(default=0)
next in views.py have:
class linklistview(listview): model = link x queryset = link.with_votes.all()
this setup following: whenever url submitted or voted upon, linkvotecountmanager()
sums up/down votes , returns query set ordered total.
now freshly submitted link, linkvotecountmanager()
should sum votes 0, since value
in vote
being set 0 (by default). but instead, i'm getting none
vote count of freshly submitted links, totally messes how votes ordered. why linkvotecountmanager
behaving incorrectly fresh links (yet working after first vote has been cast). , how can fix it? please advise.
there no vote
objects newly created links. in case, annotated votes
attribute none
. don't know straight-forward way of fixing right now, guess can solve using f expressions.
Comments
Post a Comment