相关疑难解决方法(0)

(Django)修剪charField的空格

如何从Django中的charField末尾删除空格(trim)?

这是我的模型,你可以看到我已经尝试过使用干净的方法,但这些方法永远不会运行.

我也尝试过name.strip(),models.charField().strip()但这些都不起作用.

有没有办法强制charField自动修剪?

谢谢.

from django.db import models
from django.forms import ModelForm
from django.core.exceptions import ValidationError
import datetime

class Employee(models.Model):
    """(Workers, Staff, etc)"""
    name                = models.CharField(blank=True, null=True, max_length=100)

    def save(self, *args, **kwargs):
        try:
            # This line doesn't do anything??
            #self.full_clean()
            Employee.clean(self)
        except ValidationError, e:
            print e.message_dict

        super(Employee, self).save(*args, **kwargs) # Real save

    # If I uncomment this, I get an TypeError: unsubscriptable object
    #def clean(self):
    #   return self.clean['name'].strip()

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural …
Run Code Online (Sandbox Code Playgroud)

python django django-models removing-whitespace

16
推荐指数
4
解决办法
3万
查看次数