You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨App导入模型触发AttributeError:找不到AbstractDownload类

Fixing AttributeError: module 'downloads.models' has no attribute 'AbstractDownload'

Let's break down what's happening here: you're hitting a circular import issue between your products and downloads apps. Looking at the traceback, your downloads/models.py is importing products/models at the module level, while products/models.py is importing downloads/models to inherit from AbstractDownload. When Django tries to load these models, it gets stuck in a loop—by the time products/models tries to access AbstractDownload, the downloads/models module hasn't finished defining that class yet.

Here are a few straightforward fixes:

1. Move the cross-app import inside a method (instead of module-level)

If your downloads/models.py only uses products_models in a specific method (not at the top level for model definitions), move the import into that method. For example:

Before (in downloads/models.py):

from products import models as products_models

class SomeDownloadRelatedModel(models.Model):
    # ... fields ...
    def some_method(self):
        # uses products_models here

After:

class SomeDownloadRelatedModel(models.Model):
    # ... fields ...
    def some_method(self):
        from products import models as products_models
        # Now use products_models here

This way, the import only happens when the method is called, not during the initial model loading phase that's causing the loop.

2. Use Django's string-based model references (if applicable)

If you're referencing a products model in downloads/models.py via a field like ForeignKey, use a string path instead of importing the model directly. For example:

# Instead of:
from products.models import Product
product = models.ForeignKey(Product, ...)

# Use:
product = models.ForeignKey('products.Product', ...)

Django resolves these string references lazily, which avoids the circular import issue.

3. Refactor shared abstract classes to a common app

If you have other shared code between these apps, consider creating a small common app to hold abstract models like AbstractDownload. Then both products and downloads can import from common.models instead of each other:

  • Create a common app and add it to INSTALLED_APPS
  • Move AbstractDownload to common/models.py
  • Update imports:
    • In products/models.py: from common.models import AbstractDownload
    • In downloads/models.py: Remove the products import (if no longer needed) or adjust to use string references/common imports

Verify the fix

After making your chosen change, run python manage.py runserver or python manage.py makemigrations to confirm the error is gone.

内容的提问来源于stack exchange,提问作者user9192656

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:56:10