Every repository with this icon (
Every repository with this icon (
Home
Django Model Validation
Repository created for work on model validation, now an official GSOC project:
The problem
Currently there is no way to define validation logic on models.
Proposal
I would like to add hooks for validation on models and model fields so that users could define their validation logic there. This will include simple value validation (is_integer, is_email), validation spanning more fields (required_if_other_field_empty) and arbitrary user-supplied logic.
The logic defined on models will be propagated to ModelForms created from those models and will avid duplication of logic and code.
I will introduce back validators with slightly modified interface. As much logic as practical should be then moved from Forms and form Fields into validators so that the same code can be used from both Forms and Models. Validators are functions that take a value to validate and context (value of other fields when called from Form or model instance when called from Model). A proof of concept for this approach was outlined in ticket #6845.
Most of the work should be done without any effect on backwards compatibility except few cases where current behavior is broken (Forms not enforcing some restrictions on models, ability to create model without a required field etc.).
Timeline
I would like to start during EuroDjangoCon held in Prague where I will have the opportunity to gather exact requirements and expectations from core devs and also many other Django users. After this I will draft a complete design for the proposed solution identifying key features and possible bottlenecks in both design and timeline.
Next steps with basic time estimates:
- Adding hooks to model and modle fields (2 weeks)
- Modifying ModelForms to call these hooks (2 week) – this includes modifying the way models get created in ModelForms
- Moving model related validation out of ModelForms and ModelFormSets and into Model (1 week)
- Define exact interface for validators and add hooks to model and form Field classes to use validators (1 week)
- Create a mechanism for overriding default error messages in validators from forms and models (2 weeks)
- Move the validation logic from form Fields into validators seamlessly (2 weeks)
Part of the work will be covered by existing test cases testing the current ModelForms behavior. I will add test cases for newly created code and possibilities.







