如果这个问题出现在某个地方,我很抱歉,但我找不到任何东西。
所以,问题非常简单:这些是模仿request.POST.getlist('something')
行为的django原生形式吗?
在我的用户界面中,用户创建了一个他想要保存的对象列表,这些对象被表示为具有相同名称的隐藏输入列表:
<input type="hidden" name="cc" value="1045">
<input type="hidden" name="cc" value="1055">
<input type="hidden" name="cc" value="1046">
request.POST.getlist
完全符合我的要求,但我不想直接处理请求,我想通过表单来处理。
答案 0 :(得分:6)
感谢您的评论。是的,我发现ModelChoiceField
用于模型中的ManyToMany
字段。在表单方面,它表示为MultipleChoiceField/TypedMultipleChoiceField
。
所以我决定对这个字段进行子类化并覆盖validate methods
。
class NotValidatedMultipleChoiceFiled(forms.TypedMultipleChoiceField):
"""Field that do not validate if the field values are in self.choices"""
def to_python(self, value):
"""Override checking method"""
return map(self.coerce, value)
def validate(self, value):
"""Nothing to do here"""
pass