2012-04-13 79 views
1

我可以保存「節點」,「鏈接」而不是「圖形」(請參閱​​下面的錯誤)。 使用pymongo 2.1.1,Django的NoRel,Python 2.7版:在Django-NoRel中保存嵌套模型給出「無法編碼」錯誤

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 

有沒有人有任何想法又該我做??

回答

1

這裏的問題是,你不能將'set'類型的對象編碼爲BSON,因爲BSON沒有'set'類型。

最好的解決方案是在保存圖之前將set轉換爲數組。