0

たとえば、YouTube のようなビデオのコメントを評価するシステムを作成しています。コメントに対して「好きか嫌いか」(BooleanField で 1,0) を投票できます。いいねの数が多い場合、これらは上部に表示されます。 、私はビデオのコメントを取得して表示する方法を知っていますが、私の問題は、それぞれのコメントでコメントを取得しようとするときです:

私はこれらのモデルを持っています:

class Video(models.Model):
    name = models.CharField()
    #and more things

class Comment(models.Model):
    text = models.TextField(max_length=250)
    video = models.ForeignKey(Video)
    user = models.ForeignKey(User)

class RatingComment(model.Model):
    comment = models.ForeignKey(Comment)
    user = models.ForeignKey(User)
    vote = models.BooleanField()

したがって、私の意見では、次の方法でビデオのコメントを取得できます。

res = Video.objects.get(alias__exact=何か)

coms = Comment.objects.filter(video=res)

次に、この値を使用してテンプレートへの render_to_response を作成します。しかし、好き嫌いの数を含むコメントを取得する必要があります。

¿どうすればそれを作成できますか?

4

1 に答える 1

0

追加の Queryset 修飾子を使用します。

Comment.objects.select_related('ratingcomment_set').extra(
    select={
        'likes': 'SELECT COUNT(*) FROM applabel_ratingcomment WHERE applabel_ratingcomment.comment_id = applabel_comment.id and applabel_ratingcomment.vote = 1'
        'dislikes': 'SELECT COUNT(*) FROM applabel_ratingcomment WHERE applabel_ratingcomment.comment_id = applabel_comment.id and applabel_ratingcomment.vote = 0'
    },
)
于 2012-11-27T17:50:46.400 に答える