How can I store an array of strings in a Django model?

You can use cPickle...

class myClass(models.Model):
    title = models.CharField(max_length=50)
    stringArr = models.TextField()

from cPickle import loads, dumps
data = [ { 'a':'A', 'b':2, 'c':3.0 } ]
obj = Myclass.objects.get(pk=???)
# pickle data into a string-like format
obj.stringArr = dumps(data)
obj.save()
# restore original data
data = loads(obj.stringArr)

Make another model that holds a string with an optional order, give it a ForeignKey back to myClass, and store your array in there.


If you are using PostgreSQL or MongoDB(with djongo) you can do this

For PostgreSQL:

from django.contrib.postgres.fields import ArrayField

For MongoDB(with Djongo):

from djongo import models
from django.contrib.postgres.fields import ArrayField

Then

stringArr = ArrayField(models.CharField(max_length=10, blank=True),size=8)

The above works in both cases.


You can use some serialization mechanism like JSON. There's a snippet with field definition that could be of some use to you:

http://djangosnippets.org/snippets/1478/ (take a look at the code in the last comment)

With such field you can seamlessly put strings into a list and assign them to such field. The field abstraction will do the rest. The same with reading.