Django how to override clean() method in a subclass of custom form?

后端 未结 1 1362
臣服心动
臣服心动 2021-02-13 05:13

I created a custom form with custom validation like this:

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

    def clean(self):
        cleaned_data =         


        
相关标签:
1条回答
  • 2021-02-13 05:35

    You do it well, but you should load cleaned_data from super call like this:

    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
    
    0 讨论(0)
提交回复
热议问题