What is the most efficient way to store a list in the Django models?

后端 未结 12 866
执笔经年
执笔经年 2020-11-28 01:27

Currently I have a lot of python objects in my code similar to the following:

class MyClass():
  def __init__(self, name, friends):
      self.myName = name
         


        
12条回答
  •  北海茫月
    2020-11-28 01:37

    My solution, may be it helps someone:

    import json
    from django.db import models
    
    
    class ExampleModel(models.Model):
        _list = models.TextField(default='[]')
    
        @property
        def list(self):
            return json.loads(self._list)
    
        @list.setter
        def list(self, value):
            self._list = json.dumps(self.list + value)
    

提交回复
热议问题