Added Document.reload method

This commit is contained in:
Harry Marr 2010-01-05 00:25:42 +00:00
parent 5ca75e9c6d
commit 3bead80f96
3 changed files with 29 additions and 2 deletions

View File

@ -64,6 +64,14 @@ class Document(BaseDocument):
object_id = self._fields['id'].to_mongo(self.id) object_id = self._fields['id'].to_mongo(self.id)
self.__class__.objects(id=object_id).delete() 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): def validate(self):
"""Ensure that all fields' values are valid and that required fields """Ensure that all fields' values are valid and that required fields
are present. are present.

View File

@ -1,4 +1,4 @@
from setuptools import setup from setuptools import setup, find_packages
VERSION = '0.1.1' VERSION = '0.1.1'
@ -22,11 +22,12 @@ CLASSIFIERS = [
setup(name='mongoengine', setup(name='mongoengine',
version=VERSION, version=VERSION,
packages=['mongoengine'], packages=find_packages(),
author='Harry Marr', author='Harry Marr',
author_email='harry.marr@{nospam}gmail.com', author_email='harry.marr@{nospam}gmail.com',
url='http://hmarr.com/mongoengine/', url='http://hmarr.com/mongoengine/',
license='MIT', license='MIT',
include_package_data=True,
description=DESCRIPTION, description=DESCRIPTION,
long_description=LONG_DESCRIPTION, long_description=LONG_DESCRIPTION,
platforms=['any'], platforms=['any'],

View File

@ -228,6 +228,24 @@ class DocumentTest(unittest.TestCase):
self.assertEqual(person.name, "Test User") self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 30) 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): def test_dictionary_access(self):
"""Ensure that dictionary-style field access works properly. """Ensure that dictionary-style field access works properly.
""" """