Django循环导入和模型问题

时间:2017-05-26 15:17:37

标签: python django import dependencies circular-dependency

我在两个不同的应用程序中有两个Django models.py

processor app models.py

from address.models import Address
...defining clasess
class Displayble(models.Model):
 # It has no DB fields

address app models.py

from processor.models import Displayable
class Address(models.Model, Displayble):
...some fields, stored in DB

Dispalyble类移动到另一个文件是解决此依赖关系的唯一选择吗?

1 个答案:

答案 0 :(得分:5)

使用django' s Address导入apps.get_model模型。 https://docs.djangoproject.com/en/1.11/ref/applications/#django.apps.apps.get_model

processor app models.py替换

from address.models import Address
...defining clasess
class Displayble(models.Model):
# It has no DB fields

使用

from django.apps import apps
Address = apps.get_model(app_label='address', model_name='Address')
....go ahead and use Address as though imported
class Displayable(models.Model):
...