python data_error ai_generated true

django.core.exceptions.ValidationError: ['选择一个有效的选项。5不是可用选项之一。']

django.core.exceptions.ValidationError: ['Select a valid choice. 5 is not one of the available choices.']

ID: python/django-form-invalid-choice

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

带有choices的表单字段接收到不在choices列表中的值。

English

Form field with choices receives a value not in the choices list.

generic

解决方案

  1. 95% 成功率
    Ensure the submitted value is in the choices: if value in dict(choices): ...
  2. 90% 成功率
    Use a ModelChoiceField for dynamic choices.

无效尝试

常见但无效的做法:

  1. 80% 失败

    May not be a valid option; hardcoding invalid values.

  2. 70% 失败

    Loses functionality; users cannot select valid options.