我正在尝试使用django-multiselectfield创建模型,但是当我运行python manage.py runserver时,出现错误消息:AttributeError:模块'django.db.models'没有属性'TextChoices'。我已成功安装django-multiselectfield-0.1.10,但我不知道为什么会收到此错误。感谢您的帮助!
from django.db import models
from multiselectfield import MultiSelectField
class MovieGenre(models.TextChoices):
Action = 'Action'
Horror = 'Horror'
Comedy = 'Comedy'
genre = MultiSelectField(
choices=MovieGenre.choices,
max_choices=3,
min_choices=1
)
def __str__(self):
return self.question_text
您使用哪个版本的Django?
models.TextChoices是Django 3.0的新功能
Django 3.0的新增功能:TextChoices,IntegerChoices和Choices类已添加。
https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.Field.choices