model_state = state.models[app_label, self.name_lower]

 

I am trying to run

python manage.py migrate

or

python manage.py makemigrations

I got this error:

C:\Users\ce\dr1>python manage.py makemigrations TestModel
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\core\management\__init__.py",
line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\core\management\__init__.py",
line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\core\management\base.py", line
 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\core\management\base.py", line
 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\core\management\base.py", line
 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\core\management\commands\makem
igrations.py", line 141, in handle
    loader.project_state(),
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\db\migrations\loader.py", line
 324, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\db\migrations\graph.py", line
315, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\db\migrations\migration.py", l
ine 87, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "C:\Users\ce\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.2.4-py3.7.egg\django\db\migrations\operations\model
s.py", line 668, in state_forwards
    model_state = state.models[app_label, self.name_lower]
KeyError: ('TestModel', 'category')
 

 

这是由于我之前修改models内的class名称,已经在表单中,

model_state = state.models[app_label, self.name_lower]

展开migrations,会看到底下很多生成的,打开最后几个,会发现有old_name之类,因为变更名字产生的。

把含models中你想改的那些表单全删掉。或者把这些全部删掉。

然后再 python manage.py makemigrations

就会重新生成表单

上一篇:BUY LOW, BUY LOWER, POJ - 1952(LIS+去重计数)


下一篇:LeetCode 709. To Lower Case