【问题标题】:ForeignKey using child inherited classesForeignKey 使用子继承类
【发布时间】:2011-07-21 19:50:02
【问题描述】:

我想要一个模型来接受来自基础抽象类的子类。可能吗?最好的方法?

类基类(模型。模型): 元类: 摘要 = 真 A类(基类): ... B类(基类): ... C类(基类): ... 类测试(模型。模型): 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 类。

【问题讨论】:

    标签: django django-models foreign-keys


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 2014-08-08
      • 2011-08-12
      • 1970-01-01
      • 2018-05-23
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多