I want to save a list of integers as a field in my model. Since this field is not set by default in Django, I do this with a CommaSeparatedIntegerField named x. In my opinion, I take this line and create a list of integers from it. When a model instance is created with a parameter n, I want it to xbe set in length n, with each element set to zero.
Here is the model:
class Foo(models.Model):
id = models.IntegerField(default = 0)
x = models.CommaSeparatedIntegerField(max_length = 10)
@classmethod
def create(cls, _id, n):
user = cls(id = _id)
user.class_num_shown = '0,' * n
Then I create an instance:
f = Foo.create(1, 4)
f.save()
And load it from the database and convert the string to a list:
f = Foo.objects.get(id = 1)
x_string = f.x
x_list = x_string.split(',')
print x_list
But it deduces [u'0,0,0,0,'], and not what I want, it will be [0,0,0,0]. How can I achieve the desired result?