What problem does it solve?
Django models often accumulate invalid data or violate business rules. This Skill provides a robust approach to validation by combining field-level validators, cross-field checks via clean(), and database constraints to ensure data integrity across your app.
Core Features & Use Cases
- Field Validators: Use Django's built-in validators or custom ones for each field to enforce formats, ranges, and constraints.
- Cross-Field Validation: Implement model-level clean() to enforce dependencies between fields and business rules.
- Database Constraints: Define NOT NULL, UNIQUE, and CHECK constraints to enforce integrity at the database level.
- Consistency & Reusability: Centralize validation logic within models to reduce scattered validation code and prevent inconsistent rules.
- Use Case: Suppose you have a Product model with price and discount fields; ensure discount <= price and price > 0 before saving.
Quick Start
Define validators on fields, implement a clean() method for cross-field checks, and add database constraints in your Django models. For example: Create a Product model with a positive price, a discount not exceeding the price, and a non-empty SKU. Then attempt to save an invalid instance to see the validation errors.