(Django)修剪charField的空格

时间:2011-02-18 15:19:04

标签: python django django-models removing-whitespace

如何从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()

已编辑:已将代码更新为我的最新版本。我不确定我做错了什么,因为它仍然没有剥离空白(修剪)名称字段。

6 个答案:

答案 0 :(得分:21)

当您使用ModelForm实例创建/编辑模型时,保证调用模型的clean()方法。因此,如果要从字段中删除空格,只需在模型中添加clean()方法(无需编辑ModelForm类):

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

    def clean(self):
        if self.name:
            self.name = self.name.strip()

我发现以下代码片段很有用 - 它会修剪所有模型字段的空白,这些字段是CharField或TextField的子类(因此这也会捕获URLField字段),而无需单独指定字段:

def clean(self):
    for field in self._meta.fields:
        if isinstance(field, (models.CharField, models.TextField)):
            value = getattr(self, field.name)
            if value:
                setattr(self, field.name, value.strip())

有人正确地指出你不应该在名称声明中使用null = True。最佳做法是避免字符串字段为null = True,在这种情况下,上述内容简化为:

def clean(self):
    for field in self._meta.fields:
        if isinstance(field, (models.CharField, models.TextField)):
            setattr(self, field.name, getattr(self, field.name).strip())

答案 1 :(得分:11)

必须调用模型清理(它不是自动的),因此在保存方法中放置一些self.full_clean()
http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.full_clean

至于表单,您需要返回已剥离的已清理数据。

return self.cleaned_data['name'].strip()

不知何故,我认为你只是试图做一堆不起作用的东西。请记住,表单和模型是两个非常不同的东西。

查看有关如何验证表单的表单文档 http://docs.djangoproject.com/en/dev/ref/forms/validation/

super(Employee), self.clean().strip() makes no sense at all!

这是你修改的代码:

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

    def save(self, *args, **kwargs):
        self.full_clean() # performs regular validation then clean()
        super(Employee, self).save(*args, **kwargs)


    def clean(self):
        """
        Custom validation (read docs)
        PS: why do you have null=True on charfield? 
        we could avoid the check for name
        """
        if self.name: 
            self.name = self.name.strip()


class EmployeeForm(ModelForm):
    class Meta:
        model = Employee


    def clean_name(self):
        """
        If somebody enters into this form ' hello ', 
        the extra whitespace will be stripped.
        """
        return self.cleaned_data.get('name', '').strip()

答案 2 :(得分:5)

如果您要修剪这么多数据字段,为什么不尝试扩展CharField?

from django.db import models
from django.utils.translation import ugettext_lazy as _

class TrimCharField(models.CharField):
   description = _(
       "CharField that ignores leading" 
       " and trailing spaces in data")

   def get_prep_value(self, value)
       return trim(super(TrimCharField, self
           ).get_prep_value(value))

   def pre_save(self, model_instance, add):
       return trim(super(TrimCharField, self
           ).pre_save(model_instance, add))

更新: 对于Django版本< = 1.7如果要扩展字段,则使用models.SubfieldBase元类。所以这里就像:

class TrimCharField(six.with_metaclass(
    models.SubfieldBase, models.CharField)):

答案 3 :(得分:4)

Django 1.9提供了一种实现此目的的简单方法。通过使用默认为True的strip参数,可以确保修剪前导和尾随空格。您只能在表单字段中执行此操作,以确保修剪用户输入。但这仍然无法保护模型本身。如果您仍想这样做,可以使用上述任何方法。

有关详细信息,请访问https://docs.djangoproject.com/en/1.9/ref/forms/fields/#charfield

答案 4 :(得分:3)

我在视图中作为装饰器处理它。我还截断了超过CharField max_length值的字段值。

from django import forms
from django import models
from django.db.models.fields import FieldDoesNotExist
from django.utils.encoding import smart_str

class CleanCharField(forms.CharField):
        """Django's default form handling drives me nuts wrt trailing
        spaces.  http://code.djangoproject.com/attachment/ticket/6362
        """
        def clean(self, value):
            if value is None:
                value = u''
            value = smart_str(value).strip()
            value = super(forms.CharField, self).clean(value)
            return value

def truncate_charfield(model):
    """decorator to truncate CharField data to model field max_length.
    Apply to the clean method in views Form:

    @truncate_charfield(MyModel)
    def clean(self):
        ...
    """
    def wrap(f):
        def wrapped_f(*args):
            f(*args)
            d = args[0].cleaned_data
            for field in model._meta.fields:
                try:
                    mf = model._meta.get_field(field.name)
                    if isinstance(mf, models.CharField) and field.name in d:
                        d[field.name] = d[field.name][:mf.max_length]
                except FieldDoesNotExist:
                    pass
            return d
        return wrapped_f
    return wrap

答案 5 :(得分:0)

如果你仍然没有对你(和我)的Django 1.9+羞辱并将其放入你的表格中。这与@ jeremy-lewis的答案相似,但我对他有几个问题。

def clean_text_fields(self):
    # TODO: Django 1.9, use on the model strip=True
    # https://docs.djangoproject.com/en/1.9/ref/forms/fields/#charfield
    from django.forms.fields import CharField
    cd = self.cleaned_data
    for field_name, field in self.fields.items():
        if isinstance(field, CharField):
            cd[field_name] = cd[field_name].strip()
            if self.fields[field_name].required and not cd[field_name]:
                self.add_error(field_name, "This is a required field.")

def clean(self):
    self.clean_text_fields()