(Django) Trim whitespaces from charField

Posted by zardon on Stack Overflow See other posts from Stack Overflow or by zardon
Published on 2011-02-18T15:19:04Z Indexed on 2011/02/18 15:25 UTC
Read the original article Hit count: 201

How do I strip whitespaces (trim) from the end of a charField in Django?

Here is my Model, as you can see I've tried putting in clean methods but these never get run.

I've also tried doing name.strip(), models.charField().strip() but these do not work either.

Is there a way to force the charField to trim automatically for me?

Thanks.

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

    # This never gets run
    def clean_variable(self):
        data = self.cleaned_data['variable'].strip()
        return data

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Employees'

    # This never gets run either
    class EmployeesForm(forms.ModelForm):
        class Meta:
            model = Employee

        def clean_description(self):
            #if not self.cleaned_data['description'].strip():
             #   raise forms.ValidationError('Your error message here')
            self.cleaned_data['name'].strip()

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models