我试图做的是覆盖我的一个名为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)
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
我不知道发生了什么:|,任何见解我将不胜感激
答案 0 :(得分:7)
循环导入。解析它,或使用字符串作为FK参数(models.ForeignKey('app.Shastra')
)。
答案 1 :(得分:3)
看起来你有一个循环导入。
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
即使您只是从模块导入一个类,整个模块也会被执行以填充它的命名空间。