简体   繁体   English

Django中的模型集字段

[英]Model set fields in Django

I have a Django model that needs to represent a set of data. 我有一个需要表示一组数据的Django模型。 What is the canonical way to implement this? 规范的实现方式是什么? I don't see a models.SetField() in the documentation. 我在文档中没有看到models.SetField()。 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: 我不确定您要实现的目标,但是在我看来,您正在寻找一些自动获取值的getter / setter功能:

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. 然后,您可以执行obj.values = {'item1': data1} ,您的数据将自动被拾取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM