Django:使用其他模型中的字段过滤模型 [英] Django: filter a model with a field from another model

查看:57
本文介绍了Django:使用其他模型中的字段过滤模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个Django应用.我正在尝试显示多个评论的平均评分.

This is my first Django app. I am trying to display an average rating from multiple reviews.

这似乎在我的产品模板中有效,但不适用于我的博客模板,因为未定义productprofile_id:

This seems to work in my product template but not on my blog template because productprofile_id is not defined:

product_rating = Review.objects.filter(product_id=productprofile_id).aggregate(Avg('rating')).values() 

如何用Blog模型中的related_products字段过滤Review模型?还是可以仅将产品视图包含在我的帖子模板中?预先感谢您的帮助!

How do I filter the Review model with the related_products field in the Blog model? Or is it possible to just include the product view in my Post template? Thanks in advance for your help!

我的模特:

class Post(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    related_product = models.ManyToManyField(ProductProfile, blank=True)

    def __str__(self):
        return self.title

class ProductProfile(models.Model):
    product = models.CharField(max_length=80)
    avatar = models.ImageField(upload_to='profile_images', blank=True)
    price = models.IntegerField()
    info = models.TextField(blank=True)
    category = models.ForeignKey(ProductCategories)
    link = models.URLField(blank=True)
    bookmarks = models.ManyToManyField(User, blank=True,related_name='product_bookmarks')

    def __str__(self):
         return self.product

class Review(models.Model):
    user = models.ForeignKey(User)
    product = models.ForeignKey(ProductProfile)
    rating = models.IntegerField()
    review = models.TextField(blank=True)

    def __str__(self):
        return self.user.username

我的观点:

def post(request, post_id):
    try:
        post = Post.objects.get(pk=post_id)
    except Post.DoesNotExist:
        raise Http404

    product_rating = Review.objects.filter(product_id=productprofile_id).aggregate(Avg('rating')).values()

    return render(request, 'app/blog.html', {'post': post, 'product_rating': product_rating})

def product(request, productprofile_id):
    try:
        product = ProductProfile.objects.get(pk=productprofile_id)
    except ProductProfile.DoesNotExist:
        raise Http404
    product_media = ProductMedia.objects.filter(product_id=productprofile_id)
    product_brand = BrandProfile.objects.filter(products__id=productprofile_id)
    review = Review.objects.filter(product_id=productprofile_id)
    product_rating = Review.objects.filter(product_id=productprofile_id).aggregate(Avg('rating')).values()

    return render(request, 'app/product.html', {'product': product, 'product_media': product_media, 'review': review, 'product_rating': product_rating, 'product_brand': product_brand,})

我的模板标签:

{% for value in product_rating %} {{ value }} {%endfor%}

推荐答案

我认为您应该将评级作为ProductProfile模型的一种方法,该模型返回该产品的评论平均值:

I think you should make the rating a method on the ProductProfile model, which returns the average of the reviews for that product:

class ProductProfile(models.Model):
    ...
    def avg_rating(self):
        return self.review_set.aggregate(Avg('rating')).values()

现在您可以从两个视图中将其删除,并在发布视图中浏览产品时调用 {{product.avg_rating}} .

Now you can remove it from both views, and call {{ product.avg_rating }} when you iterate through the products in the post view.

这篇关于Django:使用其他模型中的字段过滤模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆