Django: Tuple / Fixed size list of model fields

I'm looking for an elegant way to store a tuple / list in a django model.

My model looks like this:

class MyClass(models.Model):

    val_0 = models.SmallIntegerField(default=0)
    val_1 = models.SmallIntegerField(default=0)
    val_2 = models.SmallIntegerField(default=0)
    val_3 = models.SmallIntegerField(default=0)
    val_4 = models.SmallIntegerField(default=0)
    val_5 = models.SmallIntegerField(default=0)
    val_6 = models.SmallIntegerField(default=0)
    val_7 = models.SmallIntegerField(default=0)
    val_8 = models.SmallIntegerField(default=0)
    val_9 = models.SmallIntegerField(default=0)

      

Is there a more convenient way to create a fixed size list of model fields?

It would be nice to access the field values ​​via my_obj.val [3]

+3


source to share


1 answer


Using PickledField:
Setting

pip install django-picklefield

      

Model



from picklefield.fields import PickledObjectField
class MyClass(models.Model):
    val = PickledObjectField(default=(0,)*10)

      

Using

obj = MyClass()
obj.val[0] = 1
obj.val[5] = 4
obj.save()
val = obj.val[0]

      

+3


source







All Articles