0

我有这些模型:

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().

当我保存数据时,还有一种方法可以保存继承吗?谢谢!

4

1 回答 1

1

根据文档,查询集仍然返回请求的模型。看这里

在您的示例中,您返回原始模型 Base 的查询集。

于 2012-08-28T14:41:42.137 回答