python - (Django) Trim whitespaces from charField -


how strip whitespaces (trim) end of charfield in django?

here model, can see i've tried putting in clean methods these never run.

i've tried doing name.strip(), models.charfield().strip() these not work either.

is there way force charfield trim automatically me?

thanks.

from django.db import models django.forms import modelform 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:             # line doesn't anything??             #self.full_clean()             employee.clean(self)         except validationerror, e:             print e.message_dict          super(employee, self).save(*args, **kwargs) # real save      # if uncomment this, 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      # have no idea if method being called or not       def full_clean(self):                return super(employee), self.clean().strip()         #return self.clean['name'].strip() 

edited: updated code latest version. not sure doing wrong it's still not stripping whitespace (trimming) name field.

model cleaning has called (it's not automatic) place self.full_clean() in save method.
http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.model.full_clean

as form, need return stripped cleaned data.

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

somehow think tried bunch of stuff doesn't work. remember forms , models 2 different things.

check on forms docs on how validate forms http://docs.djangoproject.com/en/dev/ref/forms/validation/

super(employee), self.clean().strip() makes no sense @ all!

here's code fixed:

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 clean()         super(employee, self).save(*args, **kwargs)       def clean(self):         """         custom validation (read docs)         ps: why have null=true on charfield?          avoid check name         """         if self.name:              self.name = self.name.strip()   class employeeform(modelform):     class meta:         model = employee       def clean_name(self):         """         if enters form ' hello ',          whitespace stripped.         """         return self.cleaned_data.get('name', '').strip() 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -