hmarr/mongoengine — Issue #458: FileField Losing Reference
Author: codeb2cc
Created: 2012-03-07 13:40:54+00:00
State: open

Since FileField's `__get__` method checks instance's `_data` and return a brandnew GridFSProxy, if the proxy object does not already exist, this `grid_id` is then mistakenly overwritten, and the gridfs file reference is forever lost. This means if you don't create a Document with an initial value to the FileField, you can never save gridfs reference to the document anymore(But you DO add new file to MongDB by calling `put`, `replace`, etc).

See https://gist.github.com/1993077

The output is like:

```
==== ==== Test A: Without Initial FileField Value ==== ====
New Document: 4f576109be20cc0e3f000000
Query the same document: 4f576109be20cc0e3f000000
Call replace() ...
After replace(), grid_id is 4f576109be20cc0e3f000001
Save document ...
Query again: 4f576109be20cc0e3f000000
grid_id is 4f576109be20cc0e3f000001

==== ==== Test B: Without Initial FileField Value ==== ====
New Document: 4f576109be20cc0e3f000003
Query the same document: 4f576109be20cc0e3f000003
Call replace() ...
After replace(), grid_id is 4f576109be20cc0e3f000004
Save document ...
Query again: 4f576109be20cc0e3f000003
grid_id is 4f576109be20cc0e3f000004

==== ==== MongoDB GridFS File ==== ====
[u'doc_b', u'doc_e']
```