我有两个应用程序,commonapp
和app1
.
这是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)
当我尝试从app1
或运行 SQL 时commonapp
,我收到以下错误:
$ 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.
我意识到这是一个循环依赖的问题。其他人建议将类的路径指定为字符串而不是实际的类,但这不起作用。我也不能将字符串指定为派生类中的基类名称。
如果不重构我的模型,这种循环依赖是否可能?