我不明白是否可能使用共享实例的模型之间的关系,例如我有一个使用类调用的 过程模型 区域 ,此类 (区域) 用于 发票模型内需要 ConceptDetail 的实例,并且需要 区域类 ,不过 过程类 取决于 发票 的实例,因此 过程类 正在使用 区域类 和 发票类 。
我的图表示例
程序(取决于)区域,发票
发票(取决于) ConceptDetail (谁依赖)区域
当我尝试这个时,我得到一个ImportError: cannot import name 'Area'
,如果我删除过程和发票的关系它工作正常,这是我的错误,关系创建?,我想我的修复是创建一个新的区域类为在ConceptDetail类中,不好的是它里面的数据会被复制并认为这是不对的吗?
这是我的代码
# from procedure models.py
from invoice.model import Invoice
class Area(CreateUpdateMixin):
name = models.CharField(max_length=75, verbose_name=_('Area'))
renovated = models.BooleanField(default=False)
requested = models.BooleanField(default=False)
def __str__(self):
return self.name
class Meta:
verbose_name = _('Area')
verbose_name_plural = _('Areas')
class Procedure(CreateUpdateMixin):
title = models.CharField(max_length=150, verbose_name=_('Titulo'))
area = models.ForeignKey(Area, verbose_name=_('Area'), related_name='areas')
figure = models.ForeignKey(Figure, verbose_name=_('Figura'), blank=True, null=True)
linked = models.ManyToManyField("self", verbose_name=_('Relacionados'), blank=True)
# invoices.invoice
invoices = models.ManyToManyField(Invoice, verbose_name=_('Facturas'), blank=True)
# from invoice models.py
from procedure.models import Area
class ConceptDetail(CreateUpdateMixin):
name = models.CharField(max_length=150)
area = models.ForeignKey(Area, verbose_name=_('Aplica al Area'))
concept = models.CharField(max_length=150)
fee = models.FloatField()
duty = models.FloatField()
class Invoice(CreateUpdateMixin):
#
invoice = models.CharField(max_length=15, verbose_name=_('Número de Factura'))
date = models.DateField(verbose_name=_('Fecha')) # YYYYMMDD
status = models.ForeignKey(StatusInvoice, null=True, blank=True)
来自会议 invoice_detail = models.ManyToManyField(InvoiceDetail,blank = True)
# from settings.py
INSTALLED_APPS = (
'procedure',
'invoice',
)
它打破了我的应用程序这是错误:
> Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 182, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Volumes/iMac/Users/paridin/Devel/python/pyppm/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Volumes/iMac/Users/paridin/.virtualenvs/pyppm/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Volumes/iMac/Users/paridin/.virtualenvs/pyppm/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/Volumes/iMac/Users/paridin/.virtualenvs/pyppm/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Volumes/iMac/Users/paridin/.virtualenvs/pyppm/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Volumes/iMac/Users/paridin/.virtualenvs/pyppm/lib/python3.4/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/Volumes/iMac/Users/paridin/.virtualenvs/pyppm/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Volumes/iMac/Users/paridin/Devel/python/pyppm/registration/models.py", line 4, in <module>
from procedure.models import Area
File "/Volumes/iMac/Users/paridin/Devel/python/pyppm/procedure/models.py", line 8, in <module>
from invoice.models import Invoice
File "/Volumes/iMac/Users/paridin/Devel/python/pyppm/invoice/models.py", line 5, in <module>
from procedure.models import Area
ImportError: cannot import name 'Area'
答案 0 :(得分:2)
您正在进行循环导入,导入from invoice.model import Invoice
中的procedure/models.py
和from procedure.models import Area
中的invoice/models.py
。
更改您的代码:
# procedure/models.py:
class Procedure(CreateUpdateMixin):
...
# invoices.invoice as a string:
invoices = models.ManyToManyField('invoice.Invoice',
verbose_name=_('Facturas'), blank=True)
主要概念是将班级引用改为字符串。它可以具有循环依赖性。您也可以在invoice/models.py
中执行此操作。