我有这些模型:
class Base(models.Model):
# ... attributes
def f(self):
raise Exception()
class A(Base):
attribute = models.IntegerField()
class B(A):
class Meta:
proxy = True
def f(self):
print "B", attribute
class C(A):
class Meta:
proxy = True
def f(self):
print "C", attribute
现在我和他们玩了一下,但我发现了一个问题:
c = C()
c.attribute = 1
c.save()
b = Base.objects.all()
b.f() # Aspected "C 1" to be printed, exception fired instead!
最后一行以意想不到的方式工作!因此,为了更好地查看文档,我搜索了 Django 自动向下转换属性,但我发现只有一个A()
没有自动转换的类B()
或C()
.
当我保存数据时,还有一种方法可以保存继承吗?谢谢!