Note that there are some explanatory texts on larger screens.

plurals
  1. POHacker News algorithm for django-voting sort order
    primarykey
    data
    text
    <p>I'm working on an application using <a href="http://code.google.com/p/django-voting/" rel="nofollow">django-voting</a> and have the sort order of the homepage items working using <a href="http://eflorenzano.com/blog/2008/05/24/managers-and-voting-and-subqueries-oh-my/" rel="nofollow">Eric Florenzano's custom VoteAwareManager technique</a>: </p> <p><strong>models.py</strong></p> <pre><code>class VoteAwareManager(models.Manager): """ Get top votes. hot = VoteAwareManager() """ def _get_score_annotation(self): model_type = ContentType.objects.get_for_model(self.model) table_name = self.model._meta.db_table return self.extra(select={ 'score': 'SELECT COALESCE(SUM(vote),0) FROM %s WHERE content_type_id=%d AND object_id=%s.id' % (Vote._meta.db_table, int(model_type.id), table_name) } ) def most_loved(self,): return self._get_score_annotation().order_by('-score') def most_hated(self): return self._get_score_annotation().order_by('score') class Post(models.Model): """Post model""" title = models.CharField(_("title"), max_length=200, blank=False) slug = models.SlugField(_("slug"), blank=True) author = models.ForeignKey(User, related_name="added_posts") kind = models.CharField(max_length=1, choices=KIND, default=1) url = models.URLField(blank=True, null=True, help_text="The link URL", default='') content_markdown = models.TextField(_("Entry"), blank=True) content_html = models.TextField(blank=True, null=True, editable=False) status = models.IntegerField(_("status"), choices=STATUS_CHOICES, default=IS_PUBLIC) allow_comments = models.BooleanField(_("Allow Comments?"), blank=False, default=1) created_at = models.DateTimeField(_("created at"), default=datetime.now) updated_at = models.DateTimeField(_("updated at")) objects = models.Manager() hot = VoteAwareManager() </code></pre> <p><strong>views.py</strong></p> <pre><code>def homepage(request): """Show top posts""" return object_list(request, queryset=Post.hot.most_loved().filter(status=IS_PUBLIC), template_name='homepage.html', template_object_name='post', extra_context= {'profile': get_profiles} ) </code></pre> <p><strong>I would now like to combine <a href="http://amix.dk/blog/post/19574" rel="nofollow">Hacker New's ranking algorithm</a> with the code above so that older items get moved down in rank, but I am having trouble. I am not sure if the relevant code should go into the VoteAwareManager function, or the most_loved method, or elsewhere altogether.</strong> </p> <p>The following is what I have tried: </p> <p><strong>1. Calculation in most_loved method:</strong> returns <code>TypeError at / unsupported operand type(s) for -: 'QuerySet' and 'int'</code> (when using a random timestamp just to see if I can get a result, eventually I need to figure out how to get the object timestamp, too — I am a beginning programmer):</p> <pre><code>def most_loved(self): totalscore = self._get_score_annotation() time_stamp = 20120920 gravity = 1.8 return (totalscore - 1) / pow((time_stamp+2), gravity) </code></pre> <p><strong>2. Calculation in SQL:</strong> returns <code>TemplateSyntaxError at / Caught DatabaseError while rendering: column "votes.time_stamp" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...(SELECT COALESCE(SUM(vote),0 / (EXTRACT(HOUR FROM TIME_STAMP...</code>:</p> <pre><code>class VoteAwareManager(models.Manager): """ Get top votes. hot = VoteAwareManager() """ def _get_score_annotation(self): model_type = ContentType.objects.get_for_model(self.model) table_name = self.model._meta.db_table return self.extra(select={ 'score': 'SELECT COALESCE(SUM(vote),0 / (EXTRACT(HOUR FROM TIME_STAMP)+2 * 1.8)) FROM %s WHERE content_type_id=%d AND object_id=%s.id' % (Vote._meta.db_table, int(model_type.id), table_name) } ) </code></pre> <p>One option is attempt to change the voting system to use <a href="http://code.google.com/p/django-rangevoting/" rel="nofollow">django-rangevoting</a>, but I'd like to get this working with django-voting if possible. Any help much appreciated.</p>
    singulars
    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.
    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