Custom fields with __metaclass__ = models.SubfieldBase - ValidationError breaks out of form.is_valid()
|Reported by:||Mark||Owned by:||Mark|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
According to the docstring of django.db.models.Field, django.db.models.Field.to_python "Converts the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can't be converted".
__metaclass__ = SubfieldBase for the Field subclass is supposed to force the to_python conversion of the said field without having to call clean_fields, which makes sense to do for the fields that are of no meaning to you in their textual/database representation.
Upon using it exactly in the way it is proposed (as illustrated in the attached test case), it appears, that if the validation fails and I raise the django.core.exceptions.ValidationError in to_python, the ValidationError somehow breaks out of the is_valid (where it is supposed to be caught and used to populate form.errors), if
__metaclass__ = SubfieldBase is declared in the Field subclass.
Is this a bug in documentation or, perhaps, I am doing something wrong? I would like to stress, though, that I am doing it "by the book", - exactly as it is stated in the documentation without any deviations (or so I think, anyway).
A somewhat unrelated question - how is it supposed to work?
Change History (13)
comment:1 Changed 7 years ago by
|Component:||Database layer (models, ORM) → Forms|
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Accepted|