model-validator

Implement Django model validation with field validators, clean methods, and constraints.

2|2|Updated Nov 4, 2025
One-click install
npx skills add https://github.com/gizix/cc_projects --skill model-validator
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: model-validator
Source: https://github.com/gizix/cc_projects/tree/main/django-template/.claude/skills/model-validator
Command: npx skills add https://github.com/gizix/cc_projects --skill model-validator

SYSTEM DOCUMENTATION & REQUIREMENTS

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.

Frequently Asked Questions about model-validator

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent invalid data from being saved to my Django models?

Django model validation combines field-level validators, cross-field checks in clean() methods, and database constraints. Use built-in validators like MinValueValidator and RegexValidator on fields, implement model.clean() for business rules across fields, and call full_clean() before save() to enforce all constraints before persistence.

What's the best way to validate relationships between Django model fields?

Cross-field validation uses the model's clean() method to check dependencies and business rules across multiple fields. For example, ensure a discount never exceeds a price by comparing both fields in clean(), then call full_clean() during save() to catch violations before the database.

Can I enforce numeric ranges and date validations on Django model fields?

Yes, Django's built-in validators handle numeric ranges with MinValueValidator and MaxValueValidator, and date validations through field-level validators and custom validator functions. Combine these with model.clean() for complex date logic and cross-field date comparisons.

How do I centralize validation logic across Django models to avoid duplication?

Implement custom validator functions and reusable clean_<fieldname>() methods within your model class, then call full_clean() before save(). This keeps validation rules in one place, prevents scattered code, and ensures consistent enforcement across your application.

What database constraints should I add to enforce data integrity in Django?

Define NOT NULL, UNIQUE, and CHECK constraints in your Django model's Meta class or field definitions. These database-level constraints work alongside field validators and clean() methods to enforce integrity at the storage layer, catching violations that bypass application code.

Does Django validation support custom format checks like SKU or file validation?

Yes, use RegexValidator for format checks like SKU patterns, and file or image validators for upload constraints. Combine built-in validators with custom validator functions in fields, then implement clean() for multi-field format dependencies before persisting data.