#21590: Don't require forms clean_* methods to return a value
------------------------------------------------+------------------------
Reporter: aaugustin | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Adding custom validation to forms isn't as DRY as it colud be:
{{{
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
def clean_title(self):
title = self.cleaned_data["title"]
validate_title(title) # custom validation
return title
def clean_slug(self):
slug = self.cleaned_data["slug"]
validate_slug(slug) # custom validation
return slug
}}}
The requirement that `clean()` return a `cleaned_data` dict was recently
lifted; what about doing the same for `clean_*()`? Then the example above
could be simplified to:
{{{
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
def clean_title(self):
validate_title(self.cleaned_data["title"]) # custom
validation
def clean_slug(self):
validate_slug(self.cleaned_data["slug"]) # custom validation
}}}
Otherwise developers go for private APIs, eg.:
{{{
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields["title"].validators.append(validate_title)
self.fields["slug"].validators.append(validate_slug)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/21590>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.e7446cc14692836c8c3ea66964182f94%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.