> Den 18/08/2015 kl. 10.54 skrev Erik Cederstrand <[email protected]>:
> 
> Hi list
> 
> I have a model with a field defined like this:
> 
>   categories = ArrayField(models.CharField(max_length=32))
> 
> 
> This field is accessible via the admin interface, and when I enter 
> comma-separated values in the charfield, they are stored as an array in the 
> DB.
> 
> However, I just discovered that if I enter comma+space separated values, e.g. 
> "teachers, locations, students", it gets converted to ['teachers', ' 
> locations', ' students'] which is not what I want. How go I get the 
> ArrayField to strip the values after splitting by comma?

Well, that was easy. Just subclass ArrayField:

   class StrippedArrayField(ArrayField):
       def pre_save(self, model_instance, add):
           stripped_array = [s.strip() for s in getattr(model_instance, 
self.attname)]
           setattr(model_instance, self.attname, stripped_array)
           return stripped_array


Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/775AFBB7-B637-4412-973D-A9EC95323D87%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to