首页 > 解决方案 > 如何获得观看次数最多的产品的数量?

问题描述

嗨,实际上我是 django Restframe 工作的新手。在这里我提到了我的模型我的问题是?我不知道如何存储每个产品的计数。大多数浏览的产品应该显示在前端。

模型.PY

class Products(models.Model):
   name = models.CharField(max_length=100)
   image = models.CharField(max_length=10, null=True)
   categories =  models.ArrayModelField(
       model_container=Category,
       model_form_class=CategoryForm
   )
   specifications =  models.ArrayModelField(
       model_container=Specifications,
       model_form_class=SpecificationsForm
   )
   description = models.CharField(max_length=500)
   reviews =  models.ArrayModelField(
       model_container=Reviews,
       model_form_class=ReviewsForm
   )
   drizzly = models.BooleanField(default=False)
   complete = models.BooleanField(default=False)
   comment = models.CharField(max_length=500)
   count = models.IntegerField()

标签: djangodjango-modelsdjango-formsdjango-rest-frameworkdjango-views

解决方案


如果您使用基于类的视图,您可以执行以下操作:

Class ProductsDetailView(DetailView):
    model = Products

    def get(self, **kwargs):
        self.object.count += 1
        self.object.save()
        return super(ProductsDetailView, self).get(**kwargs)

要按数量订购,请在您的模型中包括

Class Meta:
    ordering = ['count']

推荐阅读