Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem ordering by votes with django-voting
    text
    copied!<p>I have a model Post, and a model Vote. Vote (form django-voting) is essentially just a pointer to a Post and -1, 0, or 1. </p> <p>There is also Tourn, which is a start date and an end date. A Post made between the start and end of a Tourn is submitted to that tournament.</p> <p>For the sake of rep calculation, I'm trying to find the top 3 winners of a tournament. This is what I have:</p> <pre><code> posts = Post.objects.filter(status=2, created_at__range=(tourn.start_date, tourn.end_date)) start = tourn.start_date - timedelta(days=1) end = tourn.end_date + timedelta(days=1) qn = connection.ops.quote_name ctype = ContentType.objects.get_for_model(Post) posts.extra(select={'score': """ SELECT SUM(vote) FROM %s WHERE content_type_id = %s AND object_id = %s.id AND voted_at &gt; DATE(%s) AND voted_at &lt; DATE(%s) """ % (qn(Vote._meta.db_table), ctype.id, qn(Post._meta.db_table), start, end)}, order_by=['-score']) if tourn.limit_to_category: posts.filter(category=tourn.category) if len(posts) &gt;= 1: tourn_winners_1.append(posts[0].author) resp += " 1: " + posts[0].author.username + "\n" if len(posts) &gt;= 2: tourn_winners_2.append(posts[1].author) resp += " 2: " + posts[1].author.username + "\n" if len(posts) &gt;= 3: tourn_winners_3.append(posts[2].author) resp += " 3: " + posts[2].author.username + "\n" </code></pre> <p>It seems simple enough, but for some reason the results are wrong.</p> <p>The query that gets made is thus:</p> <pre><code>SELECT "blog_post"."id", "blog_post"."title", "blog_post"."slug", "blog_post"."a uthor_id", "blog_post"."creator_ip", "blog_post"."body", "blog_post"."tease", "b log_post"."status", "blog_post"."allow_comments", "blog_post"."publish", "blog_p ost"."created_at", "blog_post"."updated_at", "blog_post"."markup", "blog_post"." tags", "blog_post"."category_id" FROM "blog_post" WHERE ("blog_post"."status" = 2 AND "blog_post"."created_at" BETWEEN 2008-12-21 00:00:00 and 2009-01-04 00:00 :00) ORDER BY "blog_post"."publish" DESC </code></pre> <p>It seems that posts.extra() isn't getting applied to the query at all...</p>
 

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