Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a get_pk_dict method to conveniently retrieve a models id and its

value
  • Loading branch information...
commit 04dace3bc9f71d93bbbfd7ab27e74bb81b59875b 1 parent 6b391d5
@coleifer authored
Showing with 8 additions and 0 deletions.
  1. +3 −0  peewee.py
  2. +5 −0 tests.py
View
3  peewee.py
@@ -2740,6 +2740,9 @@ def get_or_create(cls, **query):
def get(cls, *args, **kwargs):
return cls.select().get(*args, **kwargs)
+ def get_pk_dict(self):
+ return {self._meta.pk_name: self.get_pk()}
+
def get_pk(self):
return getattr(self, self._meta.pk_name, None)
View
5 tests.py
@@ -1151,6 +1151,11 @@ def test_prepared_hook(self):
for e in Entry.raw('select * from entry'):
self.assertTrue(e._prepared)
+ def test_get_pks(self):
+ b = self.create_blog(title='b1')
+ self.assertEqual(b.get_pk_dict(), {'id': b.get_pk()})
+ self.assertFalse(b.get_pk_dict()['id'] == None)
+
class UnicodeFieldTests(BaseModelTestCase):
def get_common_objects(self):
Please sign in to comment.
Something went wrong with that request. Please try again.