Skip to content

Commit

Permalink
Added get() method to objects (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
linkyndy committed Feb 23, 2015
1 parent fbc60a0 commit 9bac6d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions remodel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def delete(self):
for field in self.fields.related:
delattr(self.fields, field)

def get(self, key, default=None):
try:
return getattr(self.fields, key)
except AttributeError:
return default

def __getitem__(self, key):
try:
return getattr(self.fields, key)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,24 @@ def test_already_deleted(self):
a.delete()

# TODO: Add tests for confirming that related objects have no reference left to the deleted object


class GetTests(BaseTestCase):
def setUp(self):
super(GetTests, self).setUp()

class Artist(Model):
pass
self.Artist = Artist

def test_existing_field(self):
a = self.Artist(name='Andrei')
assert a.get('name') == 'Andrei'

def test_inexisting_field_with_default(self):
a = self.Artist()
assert a.get('name', 'Andrei') == 'Andrei'

def test_inexisting_field_without_default(self):
a = self.Artist()
assert a.get('name') is None

0 comments on commit 9bac6d4

Please sign in to comment.