A Django form validates submitted fields and exposes cleaned values only after its declared field and form rules pass.
Django forms: ASCII wire fields and cleaned domain values
Operation contract
The amount field accepts one to six ASCII decimal digits and then converts the validated text into a positive integer. The fixture deliberately uses RegexField rather than accepting every string int() might parse. A form-wide check rejects undeclared fields because this particular wire contract allows exactly amount_minor. The program prints stable validity and cleaned-data results instead of locale-dependent error messages.
Failure and ownership boundary
Django forms do not automatically reject every extra submitted field. That policy is explicit here. The fixture validates local dictionaries; it does not render a browser form or prove CSRF protection, session identity or persistence. Never read cleaned_data as a complete accepted result before is_valid passes. Python input exercise: accept an explicit integer grammar establishes the same rule without a framework.
Tested environment
Dependency check: this program was executed on CPython 3.14.6 with Django==5.2.17. Install these versions in a separate virtual environment. The download includes the recorded environment snapshot; no third-party package is part of the website runtime.
Working program
from django.conf import settings
settings.configure(SECRET_KEY="local-fixture-only", USE_I18N=False, INSTALLED_APPS=[])
import django
django.setup()
from django import forms
class ReceiptAmountForm(forms.Form):
amount_minor = forms.RegexField(regex=r"\A[0-9]{1,6}\Z", max_length=6)
def clean_amount_minor(self):
amount = int(self.cleaned_data["amount_minor"])
if amount == 0:
raise forms.ValidationError("positive amount required")
return amount
def clean(self):
cleaned = super().clean()
if set(self.data) != {"amount_minor"}:
raise forms.ValidationError("unexpected fields")
return cleaned
accepted = ReceiptAmountForm({"amount_minor": "125"})
print(accepted.is_valid(), accepted.cleaned_data)
print(ReceiptAmountForm({"amount_minor": "125"}).is_valid())
print(ReceiptAmountForm({"amount_minor": "125", "owner": "other"}).is_valid())Output
True {'amount_minor': 125}
False
FalseCosts and limits
Field validation scans the bounded text and stores cleaned values/errors. The program has one field; large forms need their own field count, upload and validation budgets.
Common Mistakes
- Reject extra fields explicitly when the endpoint contract requires it.
- A valid form is not a CSRF or record-ownership check.
Connected lessons
Python input exercise: accept an explicit integer grammar, Django URL routing: local dispatch and parameter boundaries, Python regular expressions: use fullmatch for a complete field contract.
Related Python operation checks
Django models: database constraints survive an unchecked save, Django CSRF checks: require the token without confusing it with identity.
Follow the related contract
Django REST Framework serializers: reject coercion outside the wire contract.
