Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Forms Cleaning a Whole Form

Henrik Holmlund
Henrik Holmlund
6,113 Points

Cleaning a whole form; Not a question just a correction

You need to add an argument to the super() method. Just typing cleaned_data = super().clean() will raise an error.

In this case the following worked for me:

def clean(self):
        cleaned_data = super(SuggestionForm, self).clean()
        email = cleaned_data.get('email')
        verify = cleaned_data.get('verify_email')

        if email != verify:
            raise forms.ValidationError("The email addresses need to be the same!")

[MOD: Added python markdown formatting -cf]]

Yasser Arenas
Yasser Arenas
5,879 Points

Can someone explain me this part of code, is the function calling itself?

Jeff Muday
Jeff Muday
Treehouse Moderator 28,722 Points

Henrik, Nice work! I was coming to make a similar comment about this.

Future students may want to refer to this: https://docs.djangoproject.com/en/1.9/ref/forms/validation/

quote: "The call to super(SuggestionForm, self).clean() in the example code ensures that any validation logic in parent classes is maintained. If your form inherits another that doesn’t return a cleaned_data dictionary in its clean() method (doing so is optional), then don’t assign cleaned_data to the result of the super() call and use self.cleaned_data instead"

Jacqueline McKinney
Jacqueline McKinney
2,627 Points

I'm not getting an error by using super().clean() but I did read the documentation listed where it says to use super(SuggestionForm, self).clean(). So why is it working form me to do as the video? Kenneth Love

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You only need those arguments to super() when you're using Python 2.

In Python 3, the class name and instance (self) are implicit and only have to be provided if you need to invoke the method on a specific superclass (like a grandparent or one of the parent classes in a polymorphic class).