Django ValueError:太多的值无法解压缩(应为2)

2024-04-25 23:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用模型制作一个水滴。在

USER_TYPE = {
'admin': "Admin",
'patient': "Patient",
'helper': "Helper",
'therapist': "Therapist",
}


class User(AbstractBaseUser):
    user_type = models.CharField(max_length=10, choices=USER_TYPE, default="patient")

但是,我得到一个错误:

ValueError: too many values to unpack (expected 2)

提前谢谢!在


Tags: 模型helperadmintypeclass水滴userpatient
2条回答
  1. 您使用的是CharField,但是如果您想要下拉列表,则应该使用ChoiceField

  2. 您将用户类型作为字典提供,但是

    choices

    Either an iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field, or a callable that returns such an iterable. This argument accepts the same formats as the choices argument to a model field. See the model field reference documentation on choices for more details. If the argument is a callable, it is evaluated each time the field’s form is initialized. Defaults to an empty list. https://docs.djangoproject.com/en/1.11/ref/forms/fields/#django.forms.ChoiceField.choices

所以尝试一下:

USER_TYPE = [
('admin', "Admin"),
('patient', "Patient"),
(..., ...),
]

你只需要元组而不是字典。比如:

在学校的选择=( ('FR','Freshman'), ('SO','大二'), ('JR','Junior'), ('SR','Senior'), )在

相关问题 更多 >