2011-03-24 103 views
0

我想要一个模型接受来自基类抽象类的子类。可能吗?最好的方法来做到这一点?使用子类继承类的外键

 
class BaseClass(models.Model): 
    class Meta: 
     abstract = True 

class A(BaseClass): 
    ... 

class B(BaseClass): 
    ... 

class C(BaseClass): 
    ... 

class Test(models.Model): 
    base = models.ForeignKey(BaseClass) 

test_inst = Test.objects.get(something=something) 
b_inst = B.objects.create() 
test_inst.base = b_inst 
test_inst.save() 

另外,如果以上是可能的。那么是否有可能知道该课程是什么类型?在这个例子中,类Test中的基类必须知道它是B类的。

回答

0

抽象类没有后备存储,这意味着它没有PK,这意味着你不能有一个关系它。改为使用generic foreign key