Django threaded comments with up/down-voting (like Hacker News or Reddit) -
i'm new django. i'm trying build discussion app in django that, reddit , hacker news, threaded , sorts each comment up/down vote.
i've been using django-voting app, , continue using if @ possible.
a simplified version of models.py is:
class comment(models.model): text = models.textfield() user = models.foreignkey(user) parent = models.foreignkey('self', related_name='children', null=true, blank=true) because using django-voting app, can "score" (upvotes minus downvotes) particular comment so:
comment = comment.objects.get(pk=1) # grab comment primary key = 1 score = vote.objects.get_score(comment)["score"] what can't figure out how to: (a) prepare data in view in format turned threaded discussion in template, , (b) how in way sorted upvotes.
any on 2 issues appreciated.
i open using method, mptt, build comment tree -- still not clear how able sort upvote within django-mptt app.
thanks!
edit: have come own, extremely hack-ish solution below. i'm not going mark question answered, because don't think sort of solution 1 use in production (and hope not use in production myself). but, in case looking a solution, hope helpful:
in views.py, created function outputs sorted list vote given queryset of objects:
def list_sorted_by_score(query_set): sorted_score_list = [] # first, create list of objects associated scores object in query_set: # django-voting's get_score outputs dictionary looks like: # {"score": score, "num_votes": num_votes} score_dict = vote.objects.get_score(object) sorted_score_list.append({"object": object, "score": score_dict["score"], "num_votes": score_dict["num_votes"]}) # then, sort list of dictionaries "score" sorted_score_list.sort(key=lambda x:x["score"], reverse=true) # sorts score, highest lowest (reverse=false lowest highest) return sorted_score_list i use function sort top-level comments score. context variable includes comments, sorted score, there no parents -- i.e.:
top_level_comments = list_sorted_by_score(comment.objects.filter(parent=none)) in models.py, defined method gets list_sorted_by_score of children of given comment:
def score_list_children(self): children = self.children.all() sorted_score_list = [] object in children: score_dict = vote.objects.get_score(object) sorted_score_list.append({"object": object, "score": score_dict["score"], "num_votes": score_dict["num_votes"]}) sorted_score_list.sort(key=lambda x:x["score"], reverse=true) return sorted_score_list finally, made template single comment, "comment.html." simplified version of looks like:
<div class="comment"> {{ comment.object.text }} {# note must use comment**.object**, since that's how constructed list_sorted_by_score list of dictionaries #} {% child in comment.object.score_list_children %} {% filename="comment.html" %} {# hack around django throwing maximum recusion error #} {% include filename comment=child %} {% endwith %} {% endfor %} </div> as may evident, quite hack-ish. still interested in hearing real solutions people have tried in real world.
Comments
Post a Comment