我希望能够显示一个表,其中存储当前值以及去年的值。现在,我查询所有商店,然后遍历每个商店并从比较日期和时间获取值。但这最终需要60多个查询。
class Stores(models.Model):
storeid = models.IntegerField(default=0, primary_key=True)
location = models.CharField(max_length=128)
class ShowroomData(models.Model):
store = models.ForeignKey(Stores, db_column="storeid", default=0, on_delete=models.CASCADE)
date = models.DateField(db_index = True) # Field name made lowercase.
time = models.IntegerField() # Field name made lowercase.
sales = models.DecimalField(max_digits=10, decimal_places=2) # Field name made lowercase.
tax = models.DecimalField(max_digits=10, decimal_places=2) # Field name made lowercase.
我的views.py
for store in current_data:
comparison_data = ShowroomData.objects.filter(store=self.store, date=comparison_date, time=self.time)
if comparison_data:
for record in comparison_data:
store.compare_amount = record.sales + record.tax
我希望能够将所有这些存储在一个查询集中,然后在模板中对其进行循环,而不必在一开始就进行循环以重新追加查询。