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".
Declaring __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 5 years ago by russellm
- Component changed from Database layer (models, ORM) to Forms
- milestone set to 1.2
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:2 Changed 5 years ago by Mark
- Owner changed from nobody to Mark
- Status changed from new to assigned
Changed 5 years ago by Honza_Kral
comment:8 Changed 5 years ago by russellm
- Resolution set to fixed
- Status changed from assigned to closed