我正在尝试用Django构建eCommerce应用程序。在每个产品中,我有三个标签标签,它们是is_new、is_hot、is_promo。
发布于 2020-03-29 06:53:13
实现这些数据的方式取决于处理相关数据的方式。
is_new您需要创建date.is_hot,您需要相关的销售计数,也需要一个比较它的值,比如hot_threshold_count或something.is_promo,您可能希望这链接到促销细节。以下是我如何处理这件事的粗略草图:
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对象
https://stackoverflow.com/questions/60910829
复制相似问题