Skip to content

Commit

Permalink
Moved old legacy code to toolbox, also allowing the LegacyEmbeddedMod…
Browse files Browse the repository at this point in the history
…elField to be used to avoid converting / deconverting embedded instance fields' values -- as it works now.
  • Loading branch information
wrwrwr committed Feb 4, 2012
1 parent cb41a25 commit a561c66
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions django_mongodb_engine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@
from djangotoolbox.fields import EmbeddedModelField as _EmbeddedModelField
from django_mongodb_engine.utils import make_struct


__all__ = ['LegacyEmbeddedModelField', 'GridFSField', 'GridFSString']


class LegacyEmbeddedModelField(_EmbeddedModelField):
"""
Wrapper around djangotoolbox' :class:`EmbeddedModelField` that keeps
backwards compatibility with data generated by django-mongodb-engine < 0.3.
Wrapper around djangotoolbox' :class:`EmbeddedModelField`
that keeps backwards compatibility with data generated by
older versions of the back-end.
In version 0.2 the layout of serialized model instances changed:
the _app key is not stored anymore, while a _model name must be
accompanied by a _module key, also _id's were added automatically.
In version 0.6? we started to pass embedded instance fields' values
through back-end database type conversion / deconversion (e.g. key
value -> ObjectId).
"""
def to_python(self, values):
if isinstance(values, dict):
# In version 0.2, the layout of the serialized model instance changed.
# Cleanup up old instances from keys that aren't used any more.
values.pop('_app', None)
if '_module' not in values:
values.pop('_model', None)
# Up to version 0.2, '_id's were added automatically.
# Keep backwards compatibility to old data records.
if '_id' in values:
values['id'] = values.pop('_id')
return super(LegacyEmbeddedModelField, self).to_python(values)
pass


class GridFSField(models.Field):
"""
Expand Down

0 comments on commit a561c66

Please sign in to comment.