2

I have two files within the same folder (app) feed.

models.py

from django.db import models
from .managers import FeedManager

class Feed(models.Model):
    #fields for my model

managers.py

from django.db import models

from .models import Feed

class FeedManager(models.Manager):
    def get_queryset(self, *args, **kwargs):
        return super(FeedManager, self).get_queryset(*args, **kwargs)

I get the following error when I run the server:

      Unhandled exception in thread started by <function wrapper at 0x04064370>
Traceback (most recent call last):
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\utils\autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\core\management\commands\runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\utils\autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\utils\autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\Sumit\Desktop\django\network\feed\models.py", line 7, in <module>
    from .managers import FeedManager
  File "C:\Users\Sumit\Desktop\django\network\feed\managers.py", line 3, in <module>
    from .models import Feed
ImportError: cannot import name Feed

Why am I getting this error and how do I fix this ?

Sumit Jha
  • 2,095
  • 2
  • 21
  • 36
  • 2
    You're facing circular import issue. Remove the `.models import Feed` from the top of your `managers.py` file, and you'll be good to go. Whenever you need the model, you can import it inside the manager function itself. – Rohit Jain May 29 '16 at 08:37

1 Answers1

4

You've introduce introduced a circular import: models module depends on managers module which in turn depends on the models module.

Neither of the modules can be resolved since their dependency cannot be resolved. Try re-organizing your modules.

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114