Custom Validation with clean methods
Add field- and form-level validation rules.
Custom Validation with clean methods is a free Django Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Built-in Rules
Model fields give you basic checks, but real apps need custom rules. Django runs your own clean methods to enforce exactly the logic you want. 🧹
Field-Level clean
To validate one field, add a method named clean_fieldname. Django calls it automatically during validation for that single field.
def clean_title(self):
title = self.cleaned_data["title"]
return titleRead from cleaned_data
Inside a clean method, pull the value from cleaned_data. By this point Django has already converted it to the right Python type for you.
title = self.cleaned_data["title"]Raise to Reject
If a value breaks your rule, raise a ValidationError. Django catches it and shows the message next to that field in the form.
from django.core.exceptions import ValidationError
raise ValidationError("Title is too short.")A Real Field Check
Here we reject titles shorter than five characters. Note you must return the value at the end so it stays in cleaned_data.
def clean_title(self):
title = self.cleaned_data["title"]
if len(title) < 5:
raise ValidationError("Too short.")
return titleAlways Return the Value
The golden rule of clean_fieldname: return the cleaned value. Forget it and the field becomes None, even when input was valid.
Form-Level clean
To compare two fields, override the whole-form clean method. It runs after every field-level check has finished.
def clean(self):
cleaned = super().clean()
return cleanedCall super().clean() First
Begin your form-level clean by calling super().clean(). That gives you the full cleaned_data dict gathered from the field checks.
cleaned = super().clean()
start = cleaned.get("start")
end = cleaned.get("end")Compare Two Fields
Now you can enforce cross-field rules, like making sure end comes after start. Raise ValidationError when the relationship is wrong.
if start and end and end < start:
raise ValidationError("End must be after start.")Attach Errors to a Field
In form-level clean you can target one field with add_error. The message then appears right beside that input, not at the top.
self.add_error("end", "End must be after start.")Order of Validation
Django runs each clean_fieldname first, then the form-level clean last. Knowing this order helps you place each rule in the right spot.
Quick Check
Where should a rule that compares two fields live?
Recap: Custom Validation
Well done! Use clean_fieldname for single fields and form-level clean for cross-field rules. Raise ValidationError to reject, and always return cleaned values. 🎉
Frequently asked questions
Is the “Custom Validation with clean methods” lesson free?
Yes — the full text of “Custom Validation with clean methods” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Custom Validation with clean methods”?
Add field- and form-level validation rules. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Custom Validation with clean methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring a ModelForm and Meta.fields
- form.save() and commit=False
- Custom Validation with clean methods
- Widgets, Labels, and Help Text