如何使用来自views.py的查询集过滤Django models.py中的对象

时间:2018-07-23 21:05:00

标签: django python-3.x django-models django-templates django-views

我需要显示一个用户为该成员撰写的帖子给另一个用户留下的评分。 models.py在下面。我的models.py

中有3个功能

def average_rating(self):def total_ratings(self):def total_posts(self)所有这三个函数都使用forloop Review.objects.all():,我认为这可能是错误的。在我的views.py中,上下文为

 Review.objects.filter(review_for__username__iexact=self.kwargs.get('username'))
    return context

我认为这些视图将过滤属于被评级人的用户的评级。例如,对于Admin有4个评分2,其他用户有2个评分。如果我看到“管理员个人资料”页面。该模板应仅显示管理员2个评分的平均评分。而是显示所有4个评分的平均值(对于Admin,为2;对于其他不相关的用户,为2)。我该如何解决

下面是我的 models.py

class Review (models.Model):
        review_from = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='review_from')
        review_for = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='review_for')
        item = models.ForeignKey(OrderItem, related_name='items')
        created = models.DateTimeField(auto_now_add=True)
        feedback = models.TextField(blank=True, null=True)
        feedback_image = models.ImageField(blank=True, null=True)
        feedback_video = models.FileField(blank=True, null=True)
        rating_choices = (
            ('1', 'One'), ('2', 'Two'), ('3', 'Three'), ('4', 'Four'), ('5', 'Five'),
            ('6', 'Six'), ('7', 'Seven'), ('8', 'Eight'), ('9', 'Nine'), ('10', 'Ten')
        )
        ratings = models.CharField(max_length=2, choices=rating_choices)

        def __str__(self):
            return 'Review from {} to {} for {}'.format(self.review_from, self.review_for, self.item.tasting)

        def average_rating(self):
            counter = 0
            total_ratings = 0
            for item in Review.objects.all():
                counter += 1
                total_ratings += int(item.ratings)
                user_rating = total_ratings / counter
            return user_rating

        def total_ratings(self):
            counter = 0
            for item in Review.objects.all():
                counter += 1
            return counter

        def total_posts(self):
             for postin Review.objects.all():
                  all_posts= [post.item.tasting]
             unique_posts= set(all_posts)
        return len(unique_posts)

    class Meta:
        ordering = ['-created']

Views.py是:

def get_context_data(self, **kwargs):
    context['review_list'] = Review.objects.filter(review_for__username__iexact=self.kwargs.get('username'))
    return context

模板为:

<div class="col-md-6">
    Average rating for {{user.first_name|title}} {{user.last_name|title}} is {{user.review.average_rating}}
    {% for review in review_list|slice:":1" %}
    <b>{{ review.average_rating }}</b> based on <b>{{ review.total_ratings }}</b> ratings for {{ review.total_posts }}
    post{{ review.total_posts|pluralize }}
    <br/><br/>
    {% endfor %}
    {% for review in review_list %}
        <video width="400" controls>
          <source src="{{ review.feedback_video.url }}" type="video/mp4">
          Your browser does not support HTML5 video.
        </video><br/><br/>
    {% endfor %}
    {% for review in review_list %}
        <img src="{{ review.feedback_image.url }}" width="400px"><br/><br/>
    {% endfor %}
</div>

1 个答案:

答案 0 :(得分:0)

更改了forloop
Review.objects.all() 

for recipe in Review.objects.filter(review_for=self.review_for):