我希望在django模型中执行一个操作,在那里我提取最后10分钟的条目并对其进行一些数据处理并返回一个值。
我已经尝试过以下代码,但没有运气。对此有任何帮助都很棒。
class Data(models.Model):
time_measured = models.DateTimeField(help_text='Time the data was measured.')
int_val = models.DecimalField(max_digits=10, decimal_places=2,blank=True)
def trend(self):
range_start = self.time_measured.minute - timedelta(minute=10)
data = Data.objects.filter(time_measured__minute__range=(range_start, self.time_measured.minute))
return float(data_operation(data.int_val))
答案 0 :(得分:2)
def trend(self):
range_start = datetime.datetime.now() - timedelta(minutes=10)
data = Data.objects.filter(time_measured__gte=range_start)
答案 1 :(得分:0)
我会尝试类似的事情:
def trend(self):
range_start = self.time_measured.minute - timedelta(minute=10)
data = Data.objects.filter(time_measured__minute__gt=range_start)
return float(data_operation(data))