枚举类字段更改时引发ValueError

时间:2019-01-31 10:35:37

标签: python django django-models enums

枚举类

class QuestionValidationTag(Enum):
    FREE_TEXT = "Free text"

模型

validation_tag = models.CharField(choices=[(tag.value, tag) for tag in QuestionValidationTag],
                                      null=True, max_length=200)

我更改了枚举FREE_TEXT

class QuestionValidationTag(Enum):
        FREE_TEXT = "make free text"

之后,我尝试运行我的应用程序并引发Value Error。以及我也尝试运行python manage.py makemigrations(尽管据我所知并不需要)。但这也给了我错误。

  

提高ValueError(“%r不是有效的%s”%(值,cls。名称))

     

ValueError:“自由文本”不是有效的QuestionValidationTag

是否有任何建议可以通过更改枚举值来解决此问题?

全面的移民痕迹

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\core\management\__init__.py", line 371, in execute_
from_command_line
    utility.execute()
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\core\management\base.py", line 288, in run_from_arg
v
    self.execute(*args, **cmd_options)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\core\management\commands\makemigrations.py", line 7
8, in handle
    loader = MigrationLoader(None, ignore_no_migrations=True)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\db\migrations\loader.py", line 201, in build_graph
    self.load_disk()
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\site-packages\django\db\migrations\loader.py", line 110, in load_disk
    migration_module = import_module("%s.%s" % (module_name, migration_name))
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "D:\Robiul_Alam_Backup\habijabi_\habijabi_src\habijabi_labo\habijabi_server\apps\chatbot\migrations\0001_ini
tial.py", line 9, in <module>
    class Migration(migrations.Migration):
  File "D:\Robiul_Alam_Backup\habijabi_\habijabi_src\habijabi_labo\habijabi_server\apps\chatbot\migrations\0001_ini
tial.py", line 150, in Migration
    ('validation_tag', models.CharField(choices=[('Free text', apps.chatbot.common.enums.QuestionValidationTag('Free text')
), ('Hobby', apps.chatbot.common.enums.QuestionValidationTag('Hobby')), ('Affirmative', apps.chatbot.common.enums.QuestionV
alidationTag('Affirmative')), ('Gender', apps.chatbot.common.enums.QuestionValidationTag('Gender')), ('Age', apps.chatbot.c
ommon.enums.QuestionValidationTag('Age')), ('Drink category', apps.chatbot.common.enums.QuestionValidationTag('Drink catego
ry')), ('Chu-hi brand', apps.chatbot.common.enums.QuestionValidationTag('Chu-hi brand')), ('First impression', apps.chatbot
.common.enums.QuestionValidationTag('First impression')), ('Taste', apps.chatbot.common.enums.QuestionValidationTag('Taste'
)), ('Greetings', apps.chatbot.common.enums.QuestionValidationTag('Greetings')), ('Drinking Scene', apps.chatbot.common.enu
ms.QuestionValidationTag('Drinking Scene')), ('Drinking Frequency', apps.chatbot.common.enums.QuestionValidationTag('Drinki
ng Frequency')), ('Recent Activities', apps.chatbot.common.enums.QuestionValidationTag('Recent Activities'))], max_length=2
00, null=True)),
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\enum.py", line 291, in __call__
    return cls.__new__(cls, value)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\enum.py", line 533, in __new__
    return cls._missing_(value)
  File "D:\ROBIUL~1\HABIJABI_~1\AI_DJA~1\AI_DJA~1\lib\enum.py", line 546, in _missing_
    raise ValueError("%r is not a valid %s" % (value, cls.__name__))
ValueError: 'Free text' is not a valid QuestionValidationTag

1 个答案:

答案 0 :(得分:0)

会发生此问题,因为您已删除或更改了Enum字段中的某些值(因此旧值对该枚举不再有效),但是历史迁移之一仍在{{1 }}就是行不通的。您必须跟踪该迁移(根据stacktrace,它是Enum)并进行修复,以使枚举中的此值不再使用。