19

次のようなカスタム検証を使用してカスタム フォームを作成しました。

class MyCustomForm(forms.Form):
    # ... form fields here

    def clean(self):
        cleaned_data = self.cleaned_data
        # ... do some cross-fields validation here

        return cleaned_data

現在、このフォームは、独自の clean メソッドを持つ別のフォームでサブクラス化されています。
両方の clean() メソッドをトリガーする正しい方法は何ですか?
現時点では、これは私がしていることです:

class SubClassForm(MyCustomForm):
    # ... additional form fields here

    def clean(self):
        cleaned_data = self.cleaned_data
        # ... do some cross-fields validation for the subclass here

        # Then call the clean() method of the super  class
        super(SubClassForm, self).clean()

        # Finally, return the cleaned_data
        return cleaned_data

うまくいくようです。ただし、これにより2つの clean() メソッドが返さcleaned_dataれますが、これは少し奇妙に思えます。
これは正しい方法ですか?

4

1 に答える 1

27

あなたはそれをうまくやっていますが、次のようにスーパーコールからcleaned_dataをロードする必要があります:

class SubClassForm(MyCustomForm):
        # ... additional form fields here

    def clean(self):
        # Then call the clean() method of the super  class
        cleaned_data = super(SubClassForm, self).clean()
        # ... do some cross-fields validation for the subclass 

        # Finally, return the cleaned_data
        return cleaned_data
于 2013-04-26T09:18:33.540 に答える