如何检查产品(产品模型)是否存在这样的字段?

How do I check if such a field exists for a product (product model)?

我是django初学者.......我有这样一个模型

class Product(models.Model):
    title = models.CharField(max_length=50)
    description = models.TextField(blank=True)
    owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    slug = models.SlugField(unique=True)  # "штрих код"
    price = models.DecimalField(max_digits=8,
                                decimal_places=2, )  
    creation_date = models.DateTimeField(auto_now_add=True)   
    last_update_time = models.DateTimeField(auto_now=True)   
    image1 = models.ImageField(blank=True)
    mark = models.CharField(max_length=50, blank=True)

class Review(models.Model):
    review = models.TextField(max_length=500)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    product_connected = models.ForeignKey(Product, on_delete=models.CASCADE, 
                                          related_name="reviews")
    creation_date = models.DateTimeField(auto_now_add=True)
    last_update_time = models.DateTimeField(auto_now=True)
    rating = models.IntegerField(default=0, validators=[MinValueValidator(0), MaxValueValidator(5)])

这是这样的观点

class ProductDetailView(LoginRequiredMixin, DetailView):
    model = Product
    template_name = "product.html"
    context_object_name = "product"

    @staticmethod
    def round_custom(num, step=0.5):
        return round(num / step) * step

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)
        data["pictures_list"] = Photo.objects.filter(product_connected=self.get_object())
        data["comments"] = Review.objects.filter(product_connected=self.get_object())
        if self.request.user.is_authenticated:
            data['comment_form'] = CommentForm(instance=self.request.user)
        average_rt=Review.objects.filter(
                                  product_connected=self.get_object()).aggregate(Avg('rating'))
        avr_intermediate = str(average_rt.get("rating__avg")).replace(",", ".")
        data["average_rating"] = self.round_custom(float(avr_intermediate))
        return data

整个方案 average 需要在站点上显示评级(评级),问题是在产品模型中创建了产品,并创建了评论 class仅当用户撰写评论时。那么,我如何检查该产品是否有评论,如果有,则通过我的方案 return return data["average_rating"],如果没有,则仅 return 0 在数据平均值中。因为没有现有字段,None 来到我的代码并给出错误,如果至少有一个 Review,那么一切正常,那么评级可以转移到 Product,但是这个对我来说不是一个选择。感谢您的帮助

检查查询集大小

if len(Review.objects.filter(product_connected=self.get_object()))>0:
    #do average code
else:
    data["average_rating"]=0