如何从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 = 'Employees'
class Admin:pass
class EmployeeForm(ModelForm):
class Meta:
model = Employee
# I have no idea if this method is being called or not
def full_clean(self):
return super(Employee), self.clean().strip()
#return self.clean['name'].strip()
编辑:更新了代码到最新版本。我不确定我做错了什么,因为它仍然没有去除姓名字段的空格(修剪)。