3

我刚刚升级了 mongoengine,现在我遇到了一个错误。目前,模型定义如下:

class Descriptor(Document):
    root = ReferenceField(DescriptorNode, required=True)
    (..)

class DescriptorNode(Document):
    channel = ReferenceField(Channel, required=True)
    (..)

class Channel(Document):
    (..)

使用DBRef = True,这是我与 mongoengine==0.7.10 一起使用的当前代码:

descriptor =  Descriptor.objects.get(id = xxxx)
channel = descriptor.root.channel

我选择删除DBRef = True以便迁移到 mongoengine==0.8.1 然后重建数据库。

现在我收到此错误:

channel = descriptor.root.channel
AttributeError: channel
/site-packages/bson/dbref.py Line:88

我试过select_related()了,但似乎没有任何工作正常。

描述符

{
    "_id" : ObjectId("51ae3f6ba2aa1c0a32998952"),
    "created_date" : ISODate("2013-06-04T19:26:35.630Z"),
    "modified_date" : ISODate("2013-06-05T12:45:55.570Z"),
    "sequence" : 843174,
    "groups" : [ ],
    "name" : "desktop-BR",
    "root" : ObjectId("51ae3f6ba2aa1c0a329988b0"),
    "tree_hash" : "97e3716db74543e66a11405e9e04185452183ac1"
}

描述符节点

{
    "_id" : ObjectId("51ae3f6aa2aa1c0a3299885a"),
    "channel" : ObjectId("51ae0f06a2aa1c0a327f3958"),
    "created_date" : ISODate("2013-06-04T19:26:34.235Z"),
    "groups" : [ ],
    "is_prioritized" : false,
    "is_published" : true,
    "modified_date" : ISODate("2013-06-05T12:46:02.704Z"),
    "order" : 0,
    "sequence" : 842926
}
4

2 回答 2

3

你做了类似的例子吗?

首先,更改DBRef = TrueDBRef = False. 其次,手动迁移:

for d in Descriptor.objects():
    d.root = d.root
    d.save()

for d in DescriptorNode.objects():
    d.channel = d.channel
    d.save()
于 2013-06-04T22:43:26.637 回答
2

嗨,升级文档中有一个迁移,但这应该可以:

for d in Descriptor.objects():
    d._mark_as_changed('root')
    d.save()

for d in DescriptorNode.objects():
    d._mark_as_changed('channel')
    d.save()
于 2013-06-05T16:09:58.047 回答