Fixing delta bug for dict fields

This commit is contained in:
Ross Lawley 2011-07-29 15:48:29 +01:00
parent e3cbeb9df0
commit 3f3f93b0fa
3 changed files with 24 additions and 6 deletions

View File

@ -53,7 +53,7 @@ Changes in dev
- Added reverse delete rules - Added reverse delete rules
- Fixed issue with unset operation - Fixed issue with unset operation
- Fixed Q-object bug - Fixed Q-object bug
- Added ``QuerySet.all_fields`` resets previous .only() and .exlude() - Added ``QuerySet.all_fields`` resets previous .only() and .exclude()
- Added ``QuerySet.exclude`` - Added ``QuerySet.exclude``
- Added django style choices - Added django style choices
- Fixed order and filter issue - Fixed order and filter issue

View File

@ -721,12 +721,18 @@ class BaseDocument(object):
field = getattr(self, field_name, None) field = getattr(self, field_name, None)
if isinstance(field, EmbeddedDocument) and db_field_name not in _changed_fields: # Grab all embedded fields that have been changed if isinstance(field, EmbeddedDocument) and db_field_name not in _changed_fields: # Grab all embedded fields that have been changed
_changed_fields += ["%s%s" % (key, k) for k in field._get_changed_fields(key) if k] _changed_fields += ["%s%s" % (key, k) for k in field._get_changed_fields(key) if k]
elif isinstance(field, (list, tuple)) and db_field_name not in _changed_fields: # Loop list fields as they contain documents elif isinstance(field, (list, tuple, dict)) and db_field_name not in _changed_fields: # Loop list / dict fields as they contain documents
for index, value in enumerate(field): # Determine the iterator to use
if not hasattr(field, 'items'):
iterator = enumerate(field)
else:
iterator = field.iteritems()
for index, value in iterator:
if not hasattr(value, '_get_changed_fields'): if not hasattr(value, '_get_changed_fields'):
continue continue
list_key = "%s%s." % (key, index) list_key = "%s%s." % (key, index)
_changed_fields += ["%s%s" % (list_key, k) for k in value._get_changed_fields(list_key) if k] _changed_fields += ["%s%s" % (list_key, k) for k in value._get_changed_fields(list_key) if k]
return _changed_fields return _changed_fields
def _delta(self): def _delta(self):
@ -736,7 +742,6 @@ class BaseDocument(object):
# Handles cases where not loaded from_son but has _id # Handles cases where not loaded from_son but has _id
doc = self.to_mongo() doc = self.to_mongo()
set_fields = self._get_changed_fields() set_fields = self._get_changed_fields()
set_data = {} set_data = {}
unset_data = {} unset_data = {}
if hasattr(self, '_changed_fields'): if hasattr(self, '_changed_fields'):
@ -775,7 +780,7 @@ class BaseDocument(object):
for p in parts: for p in parts:
if p.isdigit(): if p.isdigit():
d = d[int(p)] d = d[int(p)]
elif hasattr(d, '__getattribute__'): elif hasattr(d, '__getattribute__') and not isinstance(d, dict):
real_path = d._reverse_db_field_map.get(p, p) real_path = d._reverse_db_field_map.get(p, p)
d = getattr(d, real_path) d = getattr(d, real_path)
else: else:

View File

@ -1504,6 +1504,18 @@ class DocumentTest(unittest.TestCase):
del(doc.embedded_field.list_field[2].list_field) del(doc.embedded_field.list_field[2].list_field)
self.assertEquals(doc._delta(), ({}, {'embedded_field.list_field.2.list_field': 1})) self.assertEquals(doc._delta(), ({}, {'embedded_field.list_field.2.list_field': 1}))
doc.save()
doc.reload()
doc.dict_field['Embedded'] = embedded_1
doc.save()
doc.reload()
doc.dict_field['Embedded'].string_field = 'Hello World'
self.assertEquals(doc._get_changed_fields(), ['dict_field.Embedded.string_field'])
self.assertEquals(doc._delta(), ({'dict_field.Embedded.string_field': 'Hello World'}, {}))
def test_delta_db_field(self): def test_delta_db_field(self):
class Doc(Document): class Doc(Document):
@ -1795,7 +1807,8 @@ class DocumentTest(unittest.TestCase):
person.save() person.save()
person = self.Person.objects.get() person = self.Person.objects.get()
self.assertTrue(person.comments_dict['first_post'].published) self.assertFalse(person.comments_dict['first_post'].published)
def test_delete(self): def test_delete(self):
"""Ensure that document may be deleted using the delete method. """Ensure that document may be deleted using the delete method.
""" """