【问题标题】:Child dependant field in abstract class with Django's ORM带有Django ORM的抽象类中的子依赖字段
【发布时间】:2012-11-04 21:35:37
【问题描述】:

我有这两个模型:

class Interface(models.Model):
    # fields
class OldInterface(models.Model):
    interface = models.ForeignKey(_base,related_name='old_versions')
    class Meta:
        abstract = True

由它们的“空间”和“功能”变体继承:

class SpatialInterface(Interface):
    # fields
class OldSpatialInterface(OldInterface):
    _base = SpatialInterface

class FunctionalInterface(Interface):
    # fields
class OldFunctionalInterface(OldInterface):
    _base = FunctionalInterface

当然我的代码失败了,因为_base 没有在父模型中定义。

我的问题是:有什么办法或技巧告诉 ForeignKey 从子模型读取目标模型?

或者更一般地说,有什么方法可以在父类中动态读取位于子类中的变量?

【问题讨论】:

    标签: python inheritance django-models django-orm


    【解决方案1】:

    根据我的研究,我认为这是不可能的,因为 syncdb 解析 models.py 的方式:__init__()not 调用的,所以我没有找到任何动态的方法根据孩子设置 ForeignKey 目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-02
      • 2019-05-10
      • 2011-06-18
      • 2021-09-01
      • 1970-01-01
      • 2019-12-26
      • 2011-05-08
      相关资源
      最近更新 更多