【发布时间】:2022-01-18 20:53:08
【问题描述】:
现在有三个表:
class Product(models.Model):
sku = models.CharField(max_length=200, unique=True)
name = models.CharField(max_length=200, null=True)
class HistoricalData(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
date = models.DateTimeField()
demand_sold = models.IntegerField(default=0)
class ForecastData(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
date = models.DateTimeField()
demand_sold = models.IntegerField(default=0)
我需要比较产品列表的历史数据和预测数据并计算准确率。公式如:ForecastData.demand_sold * 100/HistoricalData.demand_sold
目前的解决方案是遍历历史和预测数据集并为demand_sold进行计算
products = Product.objects.filter(...)
queryset_hist = HistoricalData.objects.filter(product__in=products)
queryset_forecast = ForecastData.objects.filter(product__in=products)
我想知道是否有任何优雅的解决方案来计算来自不同 Django 模型的字段。 (例如加入?)
【问题讨论】:
标签: django django-models django-rest-framework