如何在Django中自动更新数据?

问题描述 投票:0回答:1

我正在尝试在Django中构建电子商务应用程序。在每个产品中,我有三个标签,分别是is_newis_hotis_promo

    创建产品时,
  1. is_new将为True,我已经完成了。但是,当产品的使用期限超过7天时,系统需要自动将is_new更改为False。我在产品模型中添加了[[创建日期字段。如何将is_new自动更新为False?有任何提示吗?

  2. 当产品最畅销时,
  3. is_hot

  4. 将为True。实际上前两个产品会很热。当其他产品变热时,先前的热门产品将自动为[[False。怎么做?提示?当我为产品添加折扣时,
  5. is_promo
  6. 将为

    True

    。当我删除折扣时,它将为False。有任何提示吗?
django django-models django-views django-channels django-signals
1个回答
0
投票

[is_new您需要创建日期。

    [is_hot,您需要相关的销售数量和一个值也可以进行比较,例如hot_threshold_count之类。
  • is_promo,您可能希望将此链接链接到促销详细信息。
  • 这是我如何处理的粗略草图:
  • 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: return self.creation_datetime <= 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对象
  • © www.soinside.com 2019 - 2024. All rights reserved.