简体   繁体   中英

Weird django import errors

What I was trying to do was to override the save method of one of my models called Shastra

class Shastra(models.Model):
    something = models.IntegerField()

    def save(self, *args, **kwargs):
        post_content(app='shastra', content=self)
        super(Shastra, self).save(*args, **kwargs)


# The function being called in the override function

def post_content(*args, **kwargs):  
     FbApiContent(content = kwargs['content']).save()


# The model being used by the override function

from shastra.models import Shastra

class FbApiContent(models.Model):

    content = models.ForeignKey(Shastra)

The Traceback

Traceback (most recent call last):
  File "C:\Documents and Settings\EC.32-SAMUEL\workspace\kiosk\manage.py", line 14, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
    self.run(*args, **options)
  File "C:\Python26\lib\site-packages\django\core\management\commands\runserver.py", line 78, in run
    self.inner_run(*args, **options)
  File "C:\Python26\lib\site-packages\django\core\management\commands\runserver.py", line 88, in inner_run
    self.validate(display_num_errors=True)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python26\lib\site-packages\django\core\management\validation.py", line 36, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "C:\Python26\lib\site-packages\django\db\models\loading.py", line 146, in get_app_errors
    self._populate()
  File "C:\Python26\lib\site-packages\django\db\models\loading.py", line 64, in _populate
    self.load_app(app_name)
  File "C:\Python26\lib\site-packages\django\db\models\loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "C:\Python26\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "C:\Documents and Settings\EC.32-SAMUEL\workspace\kiosk\..\kiosk\shastra\models.py", line 10, in <module>
    from fb_api.api import *
  File "C:\Documents and Settings\EC.32-SAMUEL\workspace\kiosk\..\kiosk\fb_api\api.py", line 7, in <module>
    from fb_api.models import FbApiUser
  File "C:\Documents and Settings\EC.32-SAMUEL\workspace\kiosk\..\kiosk\fb_api\models.py", line 41, in <module>
    from shastra.models import Shastra
ImportError: cannot import name Shastra

I have no idea what's going on : |, any insights I would be grateful

Circular import. Either resolve it, or use string as FK argument ( models.ForeignKey('app.Shastra') ).

It looks like you have a circular import.

shastra\\models.py is doing from fb_api.api import *

fb_api\\api.py is doing from fb_api.models import FbApiUser

fb_api\\models.py is doing from shastra.models import Shastra

Even if you just import one class from a module, the whole module gets executed to fill in it's namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM