Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Getting Data In Correctly
Python & Django

Getting Data In Correctly

Django forms and validation at three levels, form, model and database, with business rules in the right place, usable error messages and proper tests.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

Validate at three levels — form, model and database — and put business rules where they apply to every path, not only to the form somebody happened to use.

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.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

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

More on Python & Django

Python & Django

An API Other Systems Can Depend On

Designing a Python API service others can depend on: validation at the boundary, consistent errors and status codes, early versioning and documentation.

Python & Django

Moving and Transforming Data Reliably

Building data pipelines in Python that cope with malformed input: restartable stages, quarantining failures, reconciling counts and alerting on absence.

Start here

Business rules enforced in one form only?

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

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →