#28788: Add support for Model.clean_<fieldname>

88 views
Skip to first unread message

Jonas Haag

unread,
Nov 9, 2017, 5:04:32 AM11/9/17
to django-d...@googlegroups.com
Hi,

I’m taking https://code.djangoproject.com/ticket/28788 to this mailing as per Tim’s request.

I’m convinced we should add a Model.clean_<fieldname> method, the model equivalent to Form.clean_<fieldname>.

Form validation has forms.Field.validators, Form.clean, and Form.clean_<fieldname>.
Model validation has models.Field.validators, Model.clean, but no Model.clean_<fieldname>.

Using Field.validators works, but is cumbersome and inelegant as you’ll have to put model validation logic outside the model definition.

Model.clean_<fieldname> also helps you structure your validation logic, particularly if subclasses are involved.

Backwards compatibility is an issue with my suggestion that is yet to be solved. We don’t want surprising calls to clean_XXX methods that were defined before this feature was introduced.

I’m curious to hear what everyone else thinks!

Jonas

Matthew Pava

unread,
Nov 9, 2017, 11:37:50 AM11/9/17
to django-d...@googlegroups.com

I don’t use Field.validators much myself, but it seems that the Form clean method also calls Field.validators, so the redundancy is already there in the Form class.  For consistency’s sake, I suggest we either add Model.clean_fieldname(), or we remove Form.clean_fieldname().  Both suggestions would certainly have issues with backwards compatibility.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/D7A48F7D-0A60-4ADF-B210-F73FA1F0B717%40lophus.org.
For more options, visit https://groups.google.com/d/optout.

Alasdair Nicol

unread,
Nov 9, 2017, 11:57:22 AM11/9/17
to Django developers (Contributions to Django itself)
On Thursday, 9 November 2017 16:37:50 UTC, Matthew Pava wrote:

I don’t use Field.validators much myself, but it seems that the Form clean method also calls Field.validators, so the redundancy is already there in the Form class.  For consistency’s sake, I suggest we either add Model.clean_fieldname(), or we remove Form.clean_fieldname().  Both suggestions would certainly have issues with backwards compatibility.


I don't think that Form.clean_fieldname() shouldn't be removed. Validators only check whether the value is valid or not. They do not allow you to modify the value (e.g. "return value.strip()" to remove leading/trailing whitespace).

I think that adding Model.clean_<fieldname> would be useful, and would make it more consistent with the form validation. However, backwards compatibility is an issue as Jonas mentioned.

cheers,
Alasdair

Alasdair Nicol

unread,
Nov 9, 2017, 11:58:58 AM11/9/17
to Django developers (Contributions to Django itself)


On Thursday, 9 November 2017 16:57:22 UTC, Alasdair Nicol wrote:
On Thursday, 9 November 2017 16:37:50 UTC, Matthew Pava wrote:

I don’t use Field.validators much myself, but it seems that the Form clean method also calls Field.validators, so the redundancy is already there in the Form class.  For consistency’s sake, I suggest we either add Model.clean_fieldname(), or we remove Form.clean_fieldname().  Both suggestions would certainly have issues with backwards compatibility.


I don't think that Form.clean_fieldname() shouldn't be removed.

Sorry, double negative. I meant "I think that Form.clean_fieldname() should not be removed."

Reply all
Reply to author
Forward
0 new messages