ModelMultipleChoiceField 不验证覆盖为 Select 的小部件属性
来自文档:
ManyToManyField 由 django.forms.ModelMultipleChoiceField 表示,它是一个 MultipleChoiceField,其选择是一个模型 QuerySet。
来自来源:
class ModelMultipleChoiceField(ModelChoiceField):
"""A MultipleChoiceField whose choices are a model QuerySet."""
widget = SelectMultiple
hidden_widget = MultipleHiddenInput
我开发的应用程序的设计需要 Select
小部件而不是 SelectMultiple
。
模型:
class CV(models.Model):
region = models.ManyToManyField(Region)
表单:
class RegionForm(forms.ModelForm):
class Meta:
model = CV
widgets = {'region': Select(),}
fields = ('region',)
使用上面看到的代码,模板中会引发验证错误。
这是一个错误还是我的错误?你知道解决这个问题的方法吗?
From docs:
ManyToManyField is represented by django.forms.ModelMultipleChoiceField, which is a MultipleChoiceField whose choices are a model QuerySet.
From source:
class ModelMultipleChoiceField(ModelChoiceField):
"""A MultipleChoiceField whose choices are a model QuerySet."""
widget = SelectMultiple
hidden_widget = MultipleHiddenInput
Design of the app which I develop required Select
widget instead of SelectMultiple
.
Model:
class CV(models.Model):
region = models.ManyToManyField(Region)
Form:
class RegionForm(forms.ModelForm):
class Meta:
model = CV
widgets = {'region': Select(),}
fields = ('region',)
With the code you see above the validation errors are raised in the template.
Is it a bug or my mistake? Do you know the way to fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论