diff --git a/mongoengine/document.py b/mongoengine/document.py index 61687dd7..822a3ea5 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -64,6 +64,14 @@ class Document(BaseDocument): object_id = self._fields['id'].to_mongo(self.id) self.__class__.objects(id=object_id).delete() + def reload(self): + """Reloads all attributes from the database. + """ + object_id = self._fields['id'].to_mongo(self.id) + obj = self.__class__.objects(id=object_id).first() + for field in self._fields: + setattr(self, field, getattr(obj, field)) + def validate(self): """Ensure that all fields' values are valid and that required fields are present. diff --git a/setup.py b/setup.py index e5f373ed..b8f43e1e 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup +from setuptools import setup, find_packages VERSION = '0.1.1' @@ -22,11 +22,12 @@ CLASSIFIERS = [ setup(name='mongoengine', version=VERSION, - packages=['mongoengine'], + packages=find_packages(), author='Harry Marr', author_email='harry.marr@{nospam}gmail.com', url='http://hmarr.com/mongoengine/', license='MIT', + include_package_data=True, description=DESCRIPTION, long_description=LONG_DESCRIPTION, platforms=['any'], diff --git a/tests/document.py b/tests/document.py index e977eaa0..31ae0999 100644 --- a/tests/document.py +++ b/tests/document.py @@ -228,6 +228,24 @@ class DocumentTest(unittest.TestCase): self.assertEqual(person.name, "Test User") self.assertEqual(person.age, 30) + def test_reload(self): + """Ensure that attributes may be reloaded. + """ + person = self.Person(name="Test User", age=20) + person.save() + + person_obj = self.Person.objects.first() + person_obj.name = "Mr Test User" + person_obj.age = 21 + person_obj.save() + + self.assertEqual(person.name, "Test User") + self.assertEqual(person.age, 20) + + person.reload() + self.assertEqual(person.name, "Mr Test User") + self.assertEqual(person.age, 21) + def test_dictionary_access(self): """Ensure that dictionary-style field access works properly. """