[Django] #21590: Don't require forms clean_* methods to return a value

32 views
Skip to first unread message

Django

unread,
Dec 10, 2013, 11:56:15 AM12/10/13
to django-...@googlegroups.com
#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.

Django

unread,
Dec 10, 2013, 1:37:04 PM12/10/13
to django-...@googlegroups.com
#21590: Don't require forms clean_* methods to return a value
-------------------------------------+-------------------------------------
Reporter: aaugustin | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: master
Component: Forms | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by charettes):

Maybe it was overlooked to allow `clean_*()` to return `None`?

--
Ticket URL: <https://code.djangoproject.com/ticket/21590#comment:1>

Django

unread,
Feb 9, 2014, 6:55:20 AM2/9/14
to django-...@googlegroups.com
#21590: Don't require forms clean_* methods to return a value
-------------------------------------+-------------------------------------
Reporter: aaugustin | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version: master
Component: Forms | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by mjtamlyn):

* status: new => closed
* resolution: => invalid


Comment:

We can't do this, as `None` is potentially a valid value for the field. It
is conceivable for a field to have a non-None value beforehand, and a
clean method to modify it to become None. In the case of `clean()`, this
is not a problem as `None` is not a valid value for `self.cleaned_data`.

--
Ticket URL: <https://code.djangoproject.com/ticket/21590#comment:2>

Django

unread,
Feb 9, 2014, 5:24:46 PM2/9/14
to django-...@googlegroups.com
#21590: Don't require forms clean_* methods to return a value
-------------------------------------+-------------------------------------
Reporter: aaugustin | Owner: nobody

Type: | Status: closed
Cleanup/optimization | Version: master
Component: Forms | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aaugustin):

I has a sad, but so be it...

--
Ticket URL: <https://code.djangoproject.com/ticket/21590#comment:3>

Reply all
Reply to author
Forward
0 new messages