gpt4 book ai didi

python - 当枚举类字段更改时引发 ValueError

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:57 25 4
gpt4 key购买 nike

枚举类

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"

之后,我尝试运行我的应用程序并引发值错误。我还尝试运行 python manage.py makemigrations (尽管根据我的理解,这不是必需的)。但这也给了我错误。

raise ValueError("%r is not a valid %s" % (value, cls.name))

ValueError: 'Free text' is not a valid QuestionValidationTag

有什么建议我可以通过更改枚举值来解决此问题吗?

Makemigrations 的完整踪迹

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

最佳答案

出现此问题的原因是您已删除或更改了 Enum 字段中的某些值(因此旧值对该枚举不再有效),但其中一个历史迁移仍使用该值Enum 但这是行不通的。您必须跟踪该迁移(根据堆栈跟踪,它是 0001_initial)并修复它,以便不再使用枚举中的该值。

关于python - 当枚举类字段更改时引发 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54458602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com