Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am building a Django data model and I want to be able to store an array of strings in one of the variables; how can I do that?

e.g.

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

Thanks for the help.

like image 367
Andrew Avatar asked Nov 27 '10 21:11

Andrew


1 Answers

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.

like image 79
zoldar Avatar answered Sep 19 '22 03:09

zoldar