Как хранить список в поле модели Django при использовании SQLite

Привет всем, я столкнулся с проблемой, что мне нужно вставить список [ ] в поле модели при использовании SQLite,

Group_Model.py:

class ClassificationGroup(models.Model):
   vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE, default=None, null=True)
   id = models.IntegerField(primary_key=True)
   name = models.CharField(max_length=100)
   description = models.CharField(max_length=1000)
   classification_id = models. [what do i take here to make it capable to store list of ids that will be related to the classification table.]

Надеюсь, я получу ответ на эту проблему,

Заранее спасибо.

Добавьте метод в ваш класс, чтобы преобразовать его автоматически.

import json


class ClassificationGroup(models.Model):
    #...
    classification_id = models.CharField(max_length=200)

    def set_classification_id (self, lst):
        self.classification_id = json.dumps(lst)

    def get_classification_id (self):
        return json.loads(self.classification_id)

Ваше мнение:

obj = ClassificationGroup.objects.create(name="name",**data)
obj.set_classification_id([1,2,3])
obj.save()
#there are several examples of using this method

ваш HTML:

{% for obj in objects %}
 {{ obj.name }}
 {{ obj.get_classification_id }}
{% endfor %}
Вернуться на верх