Using "id" in data not "_id" as its a mapping of fieldnames (#255)

This commit is contained in:
Ross Lawley 2013-04-17 15:07:57 +00:00
parent b4d87d9128
commit dcf3c86dce
3 changed files with 16 additions and 15 deletions

View File

@ -10,7 +10,7 @@ Changes in 0.7.10
- Added Django sessions TTL support (#224) - Added Django sessions TTL support (#224)
- Fixed issue with numerical keys in MapField(EmbeddedDocumentField()) (#240) - Fixed issue with numerical keys in MapField(EmbeddedDocumentField()) (#240)
- Fixed clearing _changed_fields for complex nested embedded documents (#237, #239, #242) - Fixed clearing _changed_fields for complex nested embedded documents (#237, #239, #242)
- Added "_id" to _data dictionary (#255) - Added "id" back to _data dictionary (#255)
- Only mark a field as changed if the value has changed (#258) - Only mark a field as changed if the value has changed (#258)
- Explicitly check for Document instances when dereferencing (#261) - Explicitly check for Document instances when dereferencing (#261)
- Fixed order_by chaining issue (#265) - Fixed order_by chaining issue (#265)

View File

@ -192,7 +192,7 @@ class BaseField(object):
return self return self
# Get value from document instance if available, if not use default # Get value from document instance if available, if not use default
value = instance._data.get(self.name or self.db_field) value = instance._data.get(self.name)
if value is None: if value is None:
value = self.default value = self.default
@ -207,9 +207,9 @@ class BaseField(object):
""" """
changed = False changed = False
if (self.name not in instance._data or if (self.name not in instance._data or
instance._data[self.name or self.db_field] != value): instance._data[self.name] != value):
changed = True changed = True
instance._data[self.name or self.db_field] = value instance._data[self.name] = value
if changed and instance._initialised: if changed and instance._initialised:
instance._mark_as_changed(self.name) instance._mark_as_changed(self.name)
@ -825,6 +825,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
if not new_class._meta.get('id_field'): if not new_class._meta.get('id_field'):
new_class._meta['id_field'] = 'id' new_class._meta['id_field'] = 'id'
new_class._fields['id'] = ObjectIdField(db_field='_id') new_class._fields['id'] = ObjectIdField(db_field='_id')
new_class._fields['id'].name = 'id'
new_class.id = new_class._fields['id'] new_class.id = new_class._fields['id']
# Merge in exceptions with parent hierarchy # Merge in exceptions with parent hierarchy
@ -915,7 +916,7 @@ class BaseDocument(object):
if name in values: if name in values:
raise TypeError("Multiple values for keyword argument '" + name + "'") raise TypeError("Multiple values for keyword argument '" + name + "'")
values[name] = value values[name] = value
signals.pre_init.send(self.__class__, document=self, values=values) signals.pre_init.send(self.__class__, document=self, values=values)
self._data = {} self._data = {}
@ -1138,7 +1139,7 @@ class BaseDocument(object):
self._changed_fields = [] self._changed_fields = []
EmbeddedDocumentField = _import_class("EmbeddedDocumentField") EmbeddedDocumentField = _import_class("EmbeddedDocumentField")
for field_name, field in self._fields.iteritems(): for field_name, field in self._fields.iteritems():
if (isinstance(field, ComplexBaseField) and if (isinstance(field, ComplexBaseField) and
isinstance(field.field, EmbeddedDocumentField)): isinstance(field.field, EmbeddedDocumentField)):
field_value = getattr(self, field_name, None) field_value = getattr(self, field_name, None)
if field_value: if field_value:
@ -1331,7 +1332,7 @@ class BaseDocument(object):
def __iter__(self): def __iter__(self):
if 'id' in self._fields and 'id' not in self._fields_ordered: if 'id' in self._fields and 'id' not in self._fields_ordered:
return iter(('id', ) + self._fields_ordered) return iter(('id', ) + self._fields_ordered)
return iter(self._fields_ordered) return iter(self._fields_ordered)
def __getitem__(self, name): def __getitem__(self, name):

View File

@ -1386,27 +1386,27 @@ class DocumentTest(unittest.TestCase):
person = self.Person(name="Test User", age=30) person = self.Person(name="Test User", age=30)
self.assertEqual(person.name, "Test User") self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 30) self.assertEqual(person.age, 30)
def test_positional_creation(self): def test_positional_creation(self):
"""Ensure that document may be created using positional arguments. """Ensure that document may be created using positional arguments.
""" """
person = self.Person("Test User", 42) person = self.Person("Test User", 42)
self.assertEqual(person.name, "Test User") self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 42) self.assertEqual(person.age, 42)
def test_mixed_creation(self): def test_mixed_creation(self):
"""Ensure that document may be created using mixed arguments. """Ensure that document may be created using mixed arguments.
""" """
person = self.Person("Test User", age=42) person = self.Person("Test User", age=42)
self.assertEqual(person.name, "Test User") self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 42) self.assertEqual(person.age, 42)
def test_bad_mixed_creation(self): def test_bad_mixed_creation(self):
"""Ensure that document gives correct error when duplicating arguments """Ensure that document gives correct error when duplicating arguments
""" """
def construct_bad_instance(): def construct_bad_instance():
return self.Person("Test User", 42, name="Bad User") return self.Person("Test User", 42, name="Bad User")
self.assertRaises(TypeError, construct_bad_instance) self.assertRaises(TypeError, construct_bad_instance)
def test_to_dbref(self): def test_to_dbref(self):
@ -1553,8 +1553,8 @@ class DocumentTest(unittest.TestCase):
self.assertEqual(2, len(keys)) self.assertEqual(2, len(keys))
self.assertTrue('e' in keys) self.assertTrue('e' in keys)
# Ensure that the _id field has the right id # Ensure that the _id field has the right id
self.assertTrue('_id' in keys) self.assertTrue('id' in keys)
self.assertEqual(doc._data.get('_id'), doc.id) self.assertEqual(doc._data.get('id'), doc.id)
def test_save(self): def test_save(self):
"""Ensure that a document may be saved in the database. """Ensure that a document may be saved in the database.
@ -3402,8 +3402,8 @@ class DocumentTest(unittest.TestCase):
Person(name="Harry Potter").save() Person(name="Harry Potter").save()
person = Person.objects.first() person = Person.objects.first()
self.assertTrue('_id' in person._data.keys()) self.assertTrue('id' in person._data.keys())
self.assertEqual(person._data.get('_id'), person.id) self.assertEqual(person._data.get('id'), person.id)
def test_complex_nesting_document_and_embedded_document(self): def test_complex_nesting_document_and_embedded_document(self):