django - 如何获得观看次数最多的产品的数量?
问题描述
嗨,实际上我是 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()
解决方案
如果您使用基于类的视图,您可以执行以下操作:
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']
推荐阅读
- java - JAVA :- 在 Linux 中不工作的多个 Java 应用程序之间的文件共享和锁定
- excel - VBA:在一列中设置范围
- python - 为什么我们需要在 bigquery 中构建数据集引用?
- docker - 在 Hypeledger Fabric 中重新启动 ca 服务器时出错
- haskell - smallcheck 属性测试中的黄金测试
- mapreduce - 当 yarn 报告 oozie 启动器和 mapreduce 作业已成功完成时,为什么 Oozie 会以错误代码 LimitExceededException 失败?
- linux - 交叉编译时如何正确链接生成文件中的共享对象(.so 文件)?
- powershell - 根据powershell中的fodlername将匹配的文件从驱动器复制到文件夹
- spring - Spring Boot Security 不会忽略某些不需要保护的路径
- firebase - Firebase 令牌是如何生成的?