Django:摆脱循环依赖

时间:2013-06-29 03:50:00

标签: django django-models

我有两个应用,commonappapp1

这是commonapp/models.py

from django.db import models
#from app1.models import SpecificFields

# Create your models here.
class CommonFields(models.Model):
    a = models.IntegerField(default = 0)

    class Meta:
        abstract = True

class SomeFields(models.Model):
#    a = models.ForeignKey(SpecificFields)
    a = models.ForeignKey('app1.models.SpecificFields')

这里是app1/models.py

from django.db import models
from commonapp.models import CommonFields

# Create your models here.
class SpecificFields(CommonFields):
    a2 = models.IntegerField(default=0)

当我尝试从app1commonapp运行SQL时,出现以下错误:

$ python manage.py sql commonapp
CommandError: One or more models did not validate:
commonapp.somefields: 'a' has a relation with model app1.models.SpecificFields,
which has either not been installed or is abstract.

我意识到这是一个循环依赖的问题。其他人建议将类的路径指定为string而不是实际的类,但这不起作用。我也不能在派生类中指定字符串作为基类名称。

如果没有重构我的模型,这样的循环依赖是否可能?

1 个答案:

答案 0 :(得分:6)

app1.modelsCommonFields之间的某处导入SomeFields