Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango threadedcomments and voting
    primarykey
    data
    text
    <p>I'm using django-threadedcomments and django-voting in my project to achieve a Reddit-like comments voting system.</p> <p>I've set everything up correctly and I'm able to successfully record votes for each threaded comment and its children, however I'm a bit stuck as to how to sort the comments so that the comment with the highest score rises to the top.</p> <p>Would passing through a template tag be a solution? I've attempted this and returned a list where the items were ordered by <code>score</code> descending, however the parent-child relationship of the comments got messed up. Here is what I did:</p> <pre><code>class OrderByVotesNode(template.Node): def __init__(self, queryset_var, direction="desc"): self.queryset_var = template.Variable(queryset_var) self.direction = direction def render(self, context): key = self.queryset_var.var value = self.queryset_var.resolve(context) try: direction = template.Variable(self.direction).resolve(context) except template.VariableDoesNotExist: direction = "desc" model = value.model qn = connection.ops.quote_name ctype = ContentType.objects.get_for_model(model) by_score = model.objects.filter(id__in=[f.id for f in value]).extra(select={"score": """ SELECT coalesce(SUM(vote), 0 ) FROM %s WHERE content_type_id = %s AND object_id = %s.%s """ % (qn(Vote._meta.db_table), ctype.id, qn(model._meta.db_table), qn(model._meta.pk.attname))}, order_by=[(direction == "desc" and "-" or "") + "score"]) context[key] = by_score return u"" </code></pre> <p>Any help or suggestions on this would be greatly appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload