django-simple-history如何将额外的信息/对象添加到历史记录?

时间:2018-11-26 13:24:37

标签: python django django-simple-history

我正在寻找一种更改历史记录中额外字段的方法。

我有一个模型InvoiceBalance

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_idNone

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]>

您知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

balance.history正在访问余额的HistoryManager类,该类本质上是历史记录模型的管理器,而不是历史记录模型实例本身。因此,行balance.history.invoice = i仅在管理器类上设置了局部变量,这没有任何作用。在用balance.save()保存余额之前,不会创建实际的历史模型实例。您要做的就是访问pre_create_historical_record信号,该信号在保存balance之后但在保存历史实例之前发送。已记录在here中。

对于您的用例,您可以为pre_create_historical_record信号设置接收器,该接收器接收history_instance并使用您想要的任何值来编辑invoice字段。您可以在基础对象上传递发票,然后像这样将其删除:

# in apps.py
def add_invoice_to_historical_balance(sender, instance, historical_instance, **kwargs):
    historical_instance.invoice = instance.invoice
    del instance.invoice

class BalanceConfig(AppConfig):
    def ready(self):
        from ..models import HistoricalUserBalance

        pre_create_historical_record.connect(
             add_invoice_to_historical_balance,
             sender=HistoricalUserBalance,
             dispatch_uid='add_invoice_to_historical_balance'
        )

您上面的代码如下:

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.invoice = i

In [18]: balance.save()