python 清理 Django 中的表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/310931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:52:47  来源:igfitidea点击:

Cleaning form data in Django

pythondjangoformsslug

提问by Josh Hunt

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.

我如何清理和修改 django 表单中的数据。我想在每个模型的每个字段的基础上定义它,就像使用 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).

我想要实现的是自动从定义的字段中删除前导和尾随空格,或者将标题(从一个字段)转换为 slug(这将是另一个字段)。

回答by Alex Gaynor

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

您可以定义可以验证和更改数据的 clean_FIELD_NAME() 方法,如下所述:http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation