How to store a dictionary on a Django Model?

前端 未结 13 933
广开言路
广开言路 2020-11-28 23:24

I need to store some data in a Django model. These data are not equal to all instances of the model.

At first I thought about subclassing the model, but I’m trying t

13条回答
  •  日久生厌
    2020-11-29 00:22

    I came to this post by google's 4rth result to "django store object"

    A little bit late, but django-picklefield looks like good solution to me.

    Example from doc:

    To use, just define a field in your model:

    >>> from picklefield.fields import PickledObjectField
    >>> class SomeObject(models.Model):
    >>>     args = PickledObjectField()
    

    and assign whatever you like (as long as it's picklable) to the field:

    >>> obj = SomeObject()
    >>> obj.args = ['fancy', {'objects': 'inside'}]
    >>> obj.save()
    

提交回复
热议问题