首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何自动更新django中的数据?

如何自动更新django中的数据?
EN

Stack Overflow用户
提问于 2020-03-29 06:21:03
回答 1查看 2K关注 0票数 0

我正在尝试用Django构建eCommerce应用程序。在每个产品中,我有三个标签标签,它们是is_newis_hotis_promo

  1. is_new将是True,当产品创建时,我已经完成了。但是,当产品使用时间超过7天时,系统需要自动将is_new转换为False。我在created_date产品模型中添加了字段。如何将is_new自动更新为False?当一个产品销售最多时,任何hints?
  2. is_hot都将是True。实际上,前两种产品都会很热。而当其他产品变热时,以前的热门产品会自动出现假。该怎么做呢?当我给产品加上折扣时,Hints?
  3. is_promo将是True。当我取消折扣时,这将是假。有什么线索吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-29 06:53:13

实现这些数据的方式取决于处理相关数据的方式。

  • is_new您需要创建date.
  • is_hot,您需要相关的销售计数,也需要一个比较它的值,比如hot_threshold_count或something.
  • is_promo,您可能希望这链接到促销细节。

以下是我如何处理这件事的粗略草图:

代码语言:javascript
复制
from django.utils import timezone
from django.conf import settings


class Product(models.Model):
    ...  # name, etc
    creation_datetime = models.Datetime(auto_add_now=True)
    sold_count = models.Integer(default=0)

    @property
    def is_hot(self) -> bool:
        return self.sold_count >= settings.HOT_COUNT_THRESHOLD

    @property
    def is_new(self) -> bool:
        elapsed_days = (timezone.now() - self.creation_datetime).days
        return elapsed_days <= settings.MAX_NEW_DAYS

    @property
    def is_promo(self) -> bool:
        has_promos = bool(Promotion.objects.filter(product=self).count())
        return has_promos



class Promotion(models.Model):
    creation_datetime = models.Datetime(auto_add_now=True)
    product = models.ForeignKey(Product)
    discount_percentage = models.Float()

其中: settings.MAX_NEW_DAYS是一个timedelta对象

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60910829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档