我有一个用于用户帖子的Post
模型,还有PostScore
模型用于跟踪Post
的得分,以便按trending
对查询集进行排序,类似reddit的'热门':
class PostScore(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
post = models.ForeignKey(Post, related_name='score')
upvotes = models.IntegerField(default=0)
downvotes = models.IntegerField(default=0)
我如何分类这些帖子?我会在PostScore
下制作自定义方法:
@property
def trending(self):
score = self.upvotes - self.downvotes
time = self.post.date
print('TIME:', time)
return score
或者我会在我的观点中对其进行排序?:
posts = Post.objects.all().filter(entered_category=category).annotate(
score_diff=F('score__upvotes') - F('score__downvotes'))\
.order_by('score_diff')
答案 0 :(得分:0)
我认为在这种情况下使用额外的Managers方法可能是个好主意。 (https://docs.djangoproject.com/en/1.11/topics/db/managers/#adding-extra-manager-methods)