Search code examples
pythondjangoformsslug

Cleaning form data in Django


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).


Solution

  • 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