Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoengine embeded document in a DynamicField

I try to embed a document into a dynamic field. But when I try to access it later, it's not a document object anymore, it's just a dict.

Here is the example code i've just made up:

#defining the documents
class Embed(EmbeddedDocument):
     field_1    = StringField(db_field='f')

 class Doc(Document):
     myid = IntField(required=True, unique=True, primary_key=True)
     embed_me = DynamicField(db_field='e')
     field_x    = StringField(db_field='x')

Then i create a new document and save it:

connect('test')

# the embedded part
embed = Embed(field_1='this is a test')

# the document with the embedded document
doc = Doc(pk=2)
doc.embed_me = embed
doc.save()

So far everything is ok. This is what I get in the db:

 # > db.doc.find()
 # { "_id" : 1, "e" : { "f" : "this is a test", "_cls" : "Embed" } }

But now, if I request the document and try to access a value from the embedded document i get an exception:

doc, c = Doc.objects.get_or_create(pk=1)

just for reference: access in the main doc works

print doc.field_x
> None

also reference: the dict looks okay, except that the names from the embedded doc are not translated

print doc.__dict__
> {'_created': False, '_data': {'myid': 1, 'embed_me': {u'_cls': u'Embed', u'f': u'this is a test'}, 'field_x': None}, '_changed_fields': [], '_initialised': True}

and now, while try to access the embedded doc, the exception rises

print doc.embed_me.field_1
>  File "embed_err.py", line 31, in <module>
print doc.embed_me.field_1
AttributeError: 'dict' object has no attribute 'field_1

what type is it?

 type(doc.embed_me)
 > <type 'dict'>

It looks like the embedded document is not translated in an object. I'm not sure if this is a bug or if I misunderstand the concept. Thanks for any advice.

like image 818
tony994 Avatar asked Oct 04 '22 05:10

tony994


1 Answers

In 0.8.3 you will have to manually reconstruct it, which is a bug - so I opened #449 and fixed in master. 0.8.4 is due later this week.

like image 92
Ross Avatar answered Oct 10 '22 16:10

Ross