开发者

Model set fields in Django

I have a Django model that needs to represent a set of data. What is the canonical way to implement this? I don't see a models.SetField() in the documentation. I could use a text field and pickling.

I recognize that this isn't the fastest way to do it, but this part of the code doesn't开发者_开发技巧 need to be super fast - simplicity is better at this point.


I'm not exactly sure what you are trying to achieve, but it seems to me your looking for some getter/setter functionality that automatically pickles the values:

import cPickle
class MyModel(models.Model):
    cpickled_values = models.TextField()

    def get_values(self):
        return cPickle.loads(str(self.cpickled_values))

    def set_values(self, value):
        self.cpickled_value = cPickle.dumps(values)

    values = property(get_values, set_values)

You can then do something like obj.values = {'item1': data1} and your data will be pickeld automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜