假设我有这样的模型:
class Account(models.Model):
balance = models.IntegerField()
debt = models.IntegerField()
history = HistoricalRecords()我使用django-simple-history来获取模型的实例,因为它会在提供的日期和时间存在:
inst = Account.history.as_of(datetime.datetime.now().date)它工作得很好,但我想获得一个实例,其中余额字段表示为它在提供的日期和时间将存在,然后债务字段将是该日期的最近。我不知道这是不是可能,我什么也没找到。
发布于 2018-01-25 06:11:11
历史ORM将返回一个基于您提交的模型的模型,因为它在那个时间点存在。
account = Account.objects.create(balance=1, debt=1)
account.save()
history_obj = account.history.last()
print(history_obj.debt) # returns 1
account.debt = 222
account.save()
new_history_obj = account.history.last()
print(new_history_obj.debt) # returns 222假设您正在使用Account.history.as_of()方法返回要从中读取的历史记录对象,您可以这样做:
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
history_obj = Account.history.as_of(yesterday)
print(history_obj.debt) # returns not the current debt, but the debt as-of yesterday除非我误解了你希望实现的目标,否则你可以用你的问题中的内容来实现这个目标:
inst = Account.history.as_of(datetime.datetime.now().date)
print(inst.debt)https://stackoverflow.com/questions/48432195
复制相似问题