MongoEngine / MongoEngine/mongoengine

DynamicDocument.reload doesn't work well

Open
#1,050 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

i encountered an issue when i was using DynamicDocument, here is the situation

In [1]: import mongoengine as models

In [2]: models.connect(port=11111)
Out[2]: MongoClient('localhost', 11111)

In [3]: class Foo(models.DynamicDocument):
    pass
   ...: 

In [4]: Foo.objects.delete()
Out[4]: 1

In [5]: foo = Foo.objects.create()

In [6]: foo._data
Out[6]: {'id': ObjectId('55910f4a53a70c17c0e2eb5e')}

In [7]: foo.update(bar=1)
Out[7]: 1

In [8]: foo._data
Out[8]: {'id': ObjectId('55910f4a53a70c17c0e2eb5e')}

In [9]: foo.reload()
Out[9]: <Foo: Foo object>

In [10]: foo._data
Out[10]: {'id': ObjectId('55910f4a53a70c17c0e2eb5e')}

In [11]: foo._dyn
foo._dynamic         foo._dynamic_fields  foo._dynamic_lock    

In [11]: foo._dynamic_fields
Out[11]: SON([])

In [12]: foo._fields_ordered
Out[12]: ('id',)

and it supposed to had bar reloaded, something like
{'bar': 1, 'id': ObjectId('55910f4a53a70c17c0e2eb5e')}

in https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/document.py#L591 we can see that it loops over self._fields_ordered, which unfortunately DynamicDocument havn't

the simplest way i think is just loop over obj._data since obj is a complete instance

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in mongoengine/document.py around line 591 and inspect how reload uses _fields_ordered for DynamicDocument instances. Compare that path with the complete instance data in _data, then verify that reloading a DynamicDocument restores dynamically added fields such as bar.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.