奇怪的 django 导入错误

发布于 2024-11-28 13:19:45 字数 3113 浏览 2 评论 0原文

我试图做的是覆盖我的一个名为 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

我不知道发生了什么:|,任何见解我将不胜感激

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-12-05 13:19:45

循环导入。要么解决它,要么使用字符串作为 FK 参数 (models.ForeignKey('app.Shastra'))。

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

骄傲 2024-12-05 13:19:45

看来您有循环导入。

shastra\models.py 正在执行 from fb_api.api import *

fb_api\api.py 正在执行 from fb_api.models import FbApiUser

fb_api\models.py 正在执行 from shastra.models import 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文