python - 保存模型时去除空格

标签 python django django-models

<分区>

Possible Duplicate:
(Django) Trim whitespaces from charField

在 ruby​​ on rails 中,保存模型时去除空格真的很容易。在 Django 中,最佳实践是什么?

最佳答案

覆盖models.Model save 方法以执行任何预先保存的操作非常容易。 stummjr 的链接也提供了一个例子。

class MyClass(models.Model):
    # some fields here

    def save(self, *args, **kwargs):
       # strip spaces here
       super(MyClass, self).save(*args, **kwargs)
       # make sure to call parent save method ^

关于python - 保存模型时去除空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11817055/

相关文章:

django - 使用 ansible 而不是 pip2 安装 pip3 包

django - 如何在 django 模板中循环使用 2 个 for 循环

python - 从 ImageField Django 2.0 获取 EXIF 数据

Django 1.10 - makemigrations 命令未检测非托管模型的更改

python - 如何获得Python的等价物[:-1] in django ORM?

Python 在两个 QThread 之间发送信号

python - 如何从 many2one 字段中删除创建和编辑...?

python - 禁止打印 pytest 的标题

python - 在 Pandas 中对一列的两行求和

python - nginx.service : Failed to read PID from file/run/nginx. pid : Invalid argument ubuntu 16. 04 xenial