如何从 Django 的下拉列表中修复此错误 "premium is not one of valid choices please select valid choice"?
How can I fix this error "premium is not one of valid choices please select valid choice" from drop down in django?
上下文:
我能够从管理面板将用户设置为 PREMIUM,但在重置数据库后我看到了这个错误。我无法弄清楚发生了什么或如何解决它。
错误是:“Select 一个有效的选择。PREMIUM 不是可用的选择之一”。
这是我个人资料的代码 class:
class Profile(models.Model):
PREMIUM_CHOICES = (("FREE",0),("PREMIUM",1))
user = models.OneToOneField(User,primary_key=True,on_delete=models.CASCADE)
premium = models.IntegerField(choices=PREMIUM_CHOICES,default=0)
due_date = models.DateField(null=True)
我在 Whosebug 上查看了几个答案:
MongoDB database. Error "Select a valid choice. That choice is not one of the available choices."
这是错误的屏幕截图:
正如 choices=…
parameter [Django-doc] 上的文档所说:
A sequence consisting itself of iterables of exactly two items (e.g.
[(A, B), (A, B) …]
) to use as choices for this
field. If choices are given, they’re enforced by model validation and
the default form widget will be a select box with these choices
instead of the standard text field.
The first element in each tuple is the actual value to be set on the
model, and the second element is the human-readable name.
因此,第一项是键(此处为 int
),第二项是该值的 human-readable 名称。因此选择应该是:
PREMIUM_CHOICES = ((<strong>0</strong>, 'FREE'),(<strong>1</strong>, 'PREMIUM'))
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
上下文:
我能够从管理面板将用户设置为 PREMIUM,但在重置数据库后我看到了这个错误。我无法弄清楚发生了什么或如何解决它。
错误是:“Select 一个有效的选择。PREMIUM 不是可用的选择之一”。
这是我个人资料的代码 class:
class Profile(models.Model):
PREMIUM_CHOICES = (("FREE",0),("PREMIUM",1))
user = models.OneToOneField(User,primary_key=True,on_delete=models.CASCADE)
premium = models.IntegerField(choices=PREMIUM_CHOICES,default=0)
due_date = models.DateField(null=True)
我在 Whosebug 上查看了几个答案: MongoDB database. Error "Select a valid choice. That choice is not one of the available choices."
这是错误的屏幕截图:
正如 choices=…
parameter [Django-doc] 上的文档所说:
A sequence consisting itself of iterables of exactly two items (e.g.
[(A, B), (A, B) …]
) to use as choices for this field. If choices are given, they’re enforced by model validation and the default form widget will be a select box with these choices instead of the standard text field.The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name.
因此,第一项是键(此处为 int
),第二项是该值的 human-readable 名称。因此选择应该是:
PREMIUM_CHOICES = ((<strong>0</strong>, 'FREE'),(<strong>1</strong>, 'PREMIUM'))
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.