Django列表的id作为表单字段

dra*_*oon 2 django django-forms

如果这个问题出现在某个地方我很抱歉,但我找不到任何东西.

所以,问题很简单:这些是模仿行为的任何django原生形式request.POST.getlist('something')吗?

在我的UI中,用户创建了一个他想要保存的对象列表,这些对象表示为具有相同名称的隐藏输入列表:

<input type="hidden" name="cc" value="1045">
<input type="hidden" name="cc" value="1055">
<input type="hidden" name="cc" value="1046">
Run Code Online (Sandbox Code Playgroud)

request.POST.getlist 完全符合我的需要,但我不想直接处理请求,我想通过表单来做.

dra*_*oon 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
Run Code Online (Sandbox Code Playgroud)