【发布时间】:2010-10-13 05:03:34
【问题描述】:
我仍然对代理模型与其在 django 中的超类的关系感到有些困惑。我现在的问题是如何从已经检索到的超类实例中获取代理模型的实例?
所以,假设我有:
class Animal(models.Model):
type = models.CharField(max_length=20)
name = models.CharField(max_length=40)
class Dog(Animal):
class Meta:
proxy = True
def make_noise(self):
print "Woof Woof"
Class Cat(Animal):
class Meta:
proxy = True
def make_noise(self):
print "Meow Meow"
animals = Animal.objects.all()
for animal in animals:
if (animal.type == "cat"):
animal_proxy = # make me a cat
elif (animal.type == "dog"):
animal_proxy = # make me a dog
animal_proxy.make_noise()
好的。所以..“#让我成为一只猫”不需要查询回数据库,例如:
animal_proxy = Cat.objects.get(id=animal.id)
有没有一种简单的方法可以从我知道是猫的 Animal 实例创建 Cat 实例?
【问题讨论】:
标签: python django django-models