在Django-NoRel中保存嵌套模型可以使#34;不能编码"错误

时间:2012-04-13 15:18:48

标签: python django mongodb pymongo django-nonrel

我可以保存“节点”,“链接”但不能保存“图表”(请参阅​​下面的错误)。 使用pymongo 2.1.1,Django-NoRel,Python 2.7:

from django.db import models
from djangotoolbox.fields import SetField, ListField, EmbeddedModelField

class Graph(models.Model):
    links = ListField(EmbeddedModelField('Link'))

class Link(models.Model):
    parent = EmbeddedModelField('Node')
    child = EmbeddedModelField('Node')

class Node(models.Model):
    extent = SetField() # set of strings e.g. "Gene-Bmp4"
    intent = SetField() # set of strings

-

n1 = Node(extent=set(["Gene-bmp4"]),intent=set(["Attr1", "Attr2"]))
n2 = Node(extent=set(["Gene-fp4"]),intent=set(["Attr3", "Attr4"]))
link = Link(parent=n1, child=n2)
links = [link]
g = Graph(links=links)
g.save()

产生错误:

/Library/Python/2.7/site-packages/pymongo-2.1.1-py2.7-macosx-10.7-intel.egg/pymongo/collection.py:312:RuntimeWarning:无法编码 - 重新加载python模块再试一次。如果你看到这个没有得到InvalidDocument异常,请参阅api.mongodb.org/python/current/faq.html#does-pymongo-work-with-mod-wsgi

Exception Type:     InvalidDocument
Exception Value:    Cannot encode object: set(['Attr2', 'Attr1'])
Exception Location:     /Library/Python/2.7/site-packages/pymongo-2.1.1-py2.7-macosx-10.7-intel.egg/pymongo/collection.py in insert, line 312

有谁知道我该怎么办?

2 个答案:

答案 0 :(得分:2)

对我来说看起来像个错误。请在https://github.com/django-nonrel/djangotoolbox

打开门票

答案 1 :(得分:1)

这里的问题是你不能将'set'类型的对象编码为BSON,因为BSON没有'set'类型。

最好的解决方案是在保存图表之前将集合转换为数组。