从其他应用程序导入模型时的AttributeError

时间:2018-01-29 10:19:39

标签: django django-models import models

我不知道为什么,但我收到错误:AttributeError: module 'downloads.models' has no attribute 'AbstractDownload'在这个地方:class Download(models_downloads.AbstractDownload):

在下载应用程序中,我已经AbstractDownload

这是我的产品型号

products/models.py
from downloads import models as models_downloads

    class Download(models_downloads.AbstractDownload):
        product = models.ForeignKey('products.Product', related_name='downloads')
        file = FilerFileField(related_name="file_products_download")

这是下载模型

downloads/models.py
class AbstractDownload(models.Model):
    title = models.CharField(max_length=500)
    subtitle = models.CharField(max_length=1000, blank=True)
    file = FilerFileField(related_name="file_abstract_download")
    order = models.PositiveIntegerField(default=0)

    class Meta:
        abstract = True

        def __str__(self):
            return self.title

回溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/path/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/path/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/path/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/path/venv/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/path/venv/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/path/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/path/www/downloads/models.py", line 6, in <module>
    from products import models as products_models
  File "/path/www/products/models.py", line 31, in <module>
    class Download(models_downloads.AbstractDownload):
AttributeError: module 'downloads.models' has no attribute 'AbstractDownload'

1 个答案:

答案 0 :(得分:0)

您有循环导入。您的downloads/models.py有:

from products import models as products_models

但是你的products/models.py

from downloads import models as models_downloads

您尚未展示如何使用products_models,因此我们无法真正说明如何修复它。以下是一些建议:

  • 如果您使用字符串作为外键,也许不需要导入,例如models.ForeignKey('downloads.ModelName')
  • 也许您可以移动AbstractDownloadDownload以便它们位于相同的模型中,这样您就不需要导入
  • 如果在方法中使用products_models,则可以在方法内移动导入。如果可能,我会避免这种情况。