简体   繁体   English

Django NameError:名称“模型”未定义

[英]Django NameError: name 'model' is not defined

I am altering an existing application so I can expand the use case a bit. 我正在更改一个现有的应用程序,以便可以扩展用例。 I have already created a new model and altered the view and template a bit. 我已经创建了一个新模型并更改了视图和模板。 Now I want to run migrations, but I keep getting the NameError and a Google search didn't provide me with much more information, especially since I am adding things to a working application rather than creating something from scratch. 现在,我想运行迁移,但是我一直收到NameError信息,而Google搜索并没有为我提供更多信息,特别是因为我是在向正在运行的应用程序中添加内容而不是从头开始创建内容。 Is anyone able to help me? 有人可以帮助我吗?

I have added the following line to admin.py: admin.site.register(InputData) 我已将以下行添加到admin.py:admin.site.register(InputData)

the error: 错误:

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

EDIT: I have solved it myself. 编辑:我自己解决了它。 Turns out there was an 'init' file where all models had to be referenced in order to use them in Admin. 原来有一个“ init”文件,必须引用所有模型才能在Admin中使用它们。

这很可能意味着您没有在admin.py文件中导入“ InputData”。

The last two lines of your error give me the idea that you might have forgotten to import 'InputData' class in admin.py file. 错误的最后两行告诉我您可能忘记了在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

Import 'InputData' class in admin.py and you are good to go :) 在admin.py中导入'InputData'类,您就可以了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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