7

How can i clean and modify data from a form in django. I would like to define it on a per field basis for each model, much like using ModelForms.

What I want to achieve is automatically remove leading and trailing spaces from defined fields, or turn a title (from one field) into a slug (which would be another field).

gak
  • 32,061
  • 28
  • 119
  • 154
Josh Hunt
  • 14,225
  • 26
  • 79
  • 98
  • How did you get leading or trailing spaces in the field? Do your users type that kind of thing? Or are you talking about making a "cleaning" pass on data you've loaded some other way? Please clarify your question. – S.Lott Nov 22 '08 at 12:40

1 Answers1

12

You can define clean_FIELD_NAME() methods which can validate and alter data, as documented here: http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation

Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113