Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
Python & Django

Getting Data In Correctly

Last updated:

Three layers, three purposes

  1. Form validation — user-facing, with helpful messages
  2. Model validation — applies to every path, including imports and scripts
  3. Database constraints — the last line, unbypassable
Business rules enforced only in a form are enforced only for people using that form. A management command, an import or an API bypasses them entirely.

Where to put a rule

Rule typeBelongs in
Field format and requiredForm and model
Cross-field business logicModel
UniquenessDatabase constraint
Referential integrityForeign key
Complex workflow rulesA service layer, called by both

Error messages people can act on

  • Say what is wrong and what would be right
  • Attach the error to the field it concerns
  • Use the business's language, not technical terms
  • Never expose internal detail in a user-facing message
  • Preserve what the user entered, so nothing is retyped

A form that clears itself on validation failure is a form people abandon, particularly on a phone.

Long forms need structure

Where a form genuinely needs many fields, split it into steps, ask the easy questions first, and show progress. Completion rates improve substantially against the same fields on one page.

Capture contact details early, so a partial completion is still something you can follow up.

Test the validation, not just the happy path

The interesting tests are the ones that submit invalid data and assert the right error appears. Testing only successful submission tests very little.

Include the boundary cases: empty, maximum length, wrong type, and the specific combinations your business rules prohibit.

Frequently asked questions

Should validation be duplicated?

Not duplicated — layered. Each layer serves a different purpose and catches a different class of problem.

Where do complex business rules go?

A service layer that both forms and other code paths call. That way the rule applies everywhere.

What about client-side validation?

Useful for immediate feedback and never a substitute for server-side validation. It is a convenience, not a control.

How do we handle validation across models?

In a service function that owns the operation, not spread across model save methods where it is hard to follow.

Keep reading

Business rules enforced in one form only?

That means imports and scripts bypass them. Worth moving them where they apply everywhere.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Custom Software DevelopmentWeb DevelopmentMachine Learning