我试图使用django-simple-history来保持对象的状态。你知道吗
假设我有以下几点:
class Parent(models.Model):
fields...
history = HistoricalRecords(inherit=True)
class Child(Parent):
fields...
class Invoice(models.Model):
fields...
parent_history = models.ForeignKey("app.HistoricalParent", blank=True, null=True, on_delete=models.PROTECT, help_text="This keeps the state of the Child when Invoice is generated")
parent = models.ForeignKey(Parent, blank=True, null=True, on_delete=models.PROTECT) # can be removed so foreign key loop gets eliminated
我怎样才能从Invoice
到达Child
?你知道吗
Invoice.objects.get(id=1).parent_history.child
无法工作和提升
AttributeError: 'HistoricalParent' object has no attribute 'child'
这就是我如何从Parent
到达Child
Invoice.objects.get(id=1).parent.child
我找不到从HistoricalChild
到HistoricalParent
的外键。我错过什么了吗?django简单的历史还有其他的作用吗?你知道吗
因此,让我在使用
django-simple-history
时中断外键关系所以
HistoricalChild
无法获得HistoricalParent
的外键将返回这么多的项目,这对我来说是非常无用的,因为父母有它的状态从某个日期,但孩子是从未来
这意味着我不能在某个时间点通过引用它的历史父代来重建一个完整的子代。。你知道吗
我最终使用
historical_date
从特定时间重新创建了一个Child
实例,如下所示我很清楚错误信息:没有与您的
Parent
模型关联的child
属性。您不能从parent
访问child
,因为它们之间没有关系(从数据库的角度来看)。从父类继承并不意味着它们之间存在任何关系,只意味着子类将从父类的属性和方法继承,仅此而已。你知道吗我不确定这是您想要做的,但是可以通过反向关系访问对象父对象。你知道吗
例如,如果在
Parent
和Child
之间有如下清晰的链接:然后,可以按如下方式访问
parent
:child.parent
(毫不奇怪),但是由于反向关系(检查related_name
参数):parent.blabla
,也可以从parent
访问child。你知道吗希望有帮助!你知道吗
相关问题 更多 >
编程相关推荐