Python Django用proxys模型复杂继承
我有这些模型:Python Django用proxys模型复杂继承,python,django,django-models,django-inheritance,Python,Django,Django Models,Django Inheritance,我有这些模型: 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 c
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 automatic downcasting属性,但我只找到了A()
类中的一个,它既没有对B()
也没有对C()
进行自动强制转换
当我保存数据时,还有一种保存继承的方法吗?
谢谢 根据文档,Queryset仍然返回请求的模型。看
在您的示例中,您返回了原始模型Base的查询集。谢谢,但无论如何,有一种方法可以识别对象的类型并对其进行强制转换?