0

コメント数が最も多い投稿順の投稿を検索する:

posts = generic_annotate(Post.objects.all(), Comment, Count('comments__id')).order_by('-score', '-created')

いいね数が最も多い投稿順の投稿を検索する:

posts = generic_annotate(Post.objects.all(), Like, Count('liking__id')).order_by('-score', '-created')

しかし、なんらかの方法でカウント(コメント+いいね)が最も多い投稿で投稿を注文する方法を考えていますが、どうすればよいかわかりません。

今私は持っています:

posts = generic_annotate(Post.objects.all(), Like, Count('liking__id'), alias='like_score')
posts = generic_annotate(posts, Comment, Count('comments__id'), alias='comment_score')
And I try to use .extra to add both fields:
posts = posts.extra(select={'fieldsum':'SUM(comment_score + like_score)'}).order_by('-fieldsum', '-created')

しかし、それらの注釈付きフィールドが存在しないというエラーが表示されます。

class Post(models.Model):
owner = models.ForeignKey(User, related_name="")
content = models.CharField()

class Like(models.Model):
sender = models.ForeignKey(User, related_name="liking")
receiver_content_type = models.ForeignKey(ContentType)
receiver_object_id = models.PositiveIntegerField()
receiver = generic.GenericForeignKey(
ct_field="receiver_content_type",
fk_field="receiver_object_id"
)
timestamp = models.DateTimeField(default=datetime.datetime.now)
class Meta:
unique_together = (
("sender", "receiver_content_type", "receiver_object_id"),
)
def __unicode__(self):
return "%s likes %s" % (self.sender, self.receiver)

## Also has the django comments associated with it.
-----------------------------------------------------------
4

0 に答える 0