#6845 - model validation status

3 views
Skip to first unread message

Honza Král

unread,
Oct 14, 2008, 9:13:22 AM10/14/08
to django-d...@googlegroups.com
Hi,
I took a great email from Gary Wilson to get me started on the status structure.

Some of the core design decisions were finalised by Jacob, Malcolm
(please scream if I have misunderstood or don't remember things
correctly) and me at DjangoCon.

Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585


* The ValidationError class has moved from forms.utils to
django.core.exceptions now that it is used for more than just forms.
ErrorList has been kept in forms.utils

validators
==========

not yet implemented - talking about plans here

* New validators (replacements for many, but not all of the old validators)
have been put in ``django.core.validators``
* the new validator is a function like:

def validate_something( value, all_values={}, instance=None ):
if i_dont_like( value ):
raise ValidationError( 'I dont like %r' % value,
error_code=validators.SOME_CODE )

the error_code can be used later in Forms to look up custom error messages.

django.db.models.Model class
============================

* validate_unique has been moved here from ModelForm

* clean and validate methods were added, the idea is that any
__validation__ should never alter the data, that's why the top-level
function that can result in data change is clean() and not validate()

* clean() calls clean() on individual fields and then calls
validate(), ValidationError with message_dict containing the errors is
raised when errors occur
* validate() calls validate_unique() and is a hook to add custom
instance-wide validation (just raise ValidationError from it)

* no validate_FIELD or clean_FIELD hooks on models
* validate is run even if some errors occur on the fields' clean() methods


Model fields (django.db.models.fields)
======================================
* clean and validate methods were added, see Model for function

Form Fields (django.forms.fields)
=================================

* form fields haven't been changed yet, I will probably try and do
the same thinng the old patch did:

* The to_python() method remains and a validate() method have been added.
Fields have had their clean() methods split into to_python() and validate()
methods.

* clean() now calls to_python() then validate().

django.forms.forms.BaseForm class
=================================

* no changes here either, so far. Will probably also try and do:

* Form gets a new validate() method, which is meant to replace clean(), and
full_clean() now looks for validate_<field> methods, which are meant to
replace clean_<field> methods. full_clean() also uses the Form.validate()
instead of Form.clean() to set the cleaned_data.

- The validate_<field> methods are passed the Field.clean()'ed value (yea
for fixing #3896).


django.forms.models.BaseModelForm class
=======================================
* save_instance has been split into two functions:
* make_instance(form, instance, fields=None, exclude=None) which
return unsaved instance with populated fields
* save_maked_instance(form, instance, fields=None, commit=True,
fail_message='saved') which saves the populated instance (and it's m2m
data)
save_instance remains and calls the two new functions

* in it's clean() method constructs the instance with make_instance,
then runs validation on it

* the validate_unique method has been moved to Model

* the form's save() method only calls save_maked_instance


django.forms.models.BaseModelFormset class
===========================================

* calls form.save() in save_new and save_existing

known Issues
=========================

* InlineFormSet.save_as_new is broken

unknown Issues
==========================
* I am fairly sure there are many since I only wrote minimal tests

I would appreciate any feedback on this, especially on:
should we change the current Form validation? - split to_python and
validate, etc..
how big an effort should we make to get all the validation to one
place - the validators (ie remove the logic from FormFields and just
have them call the validators where possible)
testing - anybody willing to write tests and/or have a piece of code
that I broke, please contact me

Reply all
Reply to author
Forward
0 new messages