如何从 Django 中的 charField 末尾去除空格(修剪)?
这是我的模型,如您所见,我尝试使用干净的方法,但这些方法永远不会运行。
我也试过做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()
已编辑:将代码更新到我的最新版本。我不确定我做错了什么,因为它仍然没有去除空格(修剪)名称字段。