我正在更改现有的应用程序,因此我可以扩展用例。我已经创建了一个新模型并更改了视图和模板。现在,我想运行迁移,但是我一直收到NameError信息,而Google搜索并没有为我提供更多信息,特别是因为我是在向正在运行的应用程序中添加内容而不是从头开始创建内容。有人可以帮助我吗?
我在admin.py中添加了以下行: admin.site.register(InputData)
错误:
C:\var\www\SYSTEM>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 120, in populate
app_config.ready()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\apps.py", line 24, in ready
self.module.autodiscover()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\var\www\SYSTEM\System\service_development\admin.py", line 201, in <module>
admin.site.register(InputData)
NameError: name 'InputData' is not defined
编辑: 我自己解决了。原来有一个“ init”文件,必须引用所有模型才能在Admin中使用它们。
答案 0 :(得分:3)
这很可能意味着您没有在admin.py文件中导入“ InputData”。
答案 1 :(得分:1)
错误的后两行告诉我,您可能忘记了在admin.py文件中导入“ InputData”类。
File "C:\var\www\SYSTEM\System\service_development\admin.py", line 201, in <module>
admin.site.register(InputData)
NameError: name 'InputData' is not defined
在admin.py中导入“ InputData”类,您很高兴:)