我正在寻找一种方法来更改历史记录中的额外字段。
我有一个模型Invoice
和Balance
.
class UserBalanceHistoricalModel(models.Model):
invoice = models.ForeignKey('Invoice', null=True, blank=True, on_delete=models.SET_NULL,
verbose_name='Faktúra')
class Meta:
abstract = True
class UserBalance(TimeStampedModel):
objects = UserBalanceManager()
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='user_balance', verbose_name='Užívateľ')
balance = models.DecimalField(max_digits=12, decimal_places=2, default=0, verbose_name="Eur na konte")
history = HistoricalRecords(bases=[UserBalanceHistoricalModel, ])
class Invoice(models.Model):
...
每次Invoice
创建对象时,都会对其UserBalance.balance
进行修改并自动创建历史记录。
我想将此Invoice
对象添加到历史记录中,但它不起作用。invoice_id
是None
。_
In [10]: u = User.objects.first()
In [11]: i = Invoice.objects.first()
In [12]: balance = u.user_balance
In [13]: balance.balance = 45
In [16]: balance.history.invoice = i
In [18]: balance.save()
In [28]: balance.history.all().values_list('invoice_id',flat=True)
Out[28]: <QuerySet [None, None, None, None]>
你知道我做错了什么吗?