Skip to content
This repository
Browse code

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

value
  • Loading branch information...
commit 04dace3bc9f71d93bbbfd7ab27e74bb81b59875b 1 parent 6b391d5
Charles Leifer authored August 01, 2012

Showing 2 changed files with 8 additions and 0 deletions. Show diff stats Hide diff stats

  1. 3  peewee.py
  2. 5  tests.py
3  peewee.py
@@ -2740,6 +2740,9 @@ def get_or_create(cls, **query):
2740 2740
     def get(cls, *args, **kwargs):
2741 2741
         return cls.select().get(*args, **kwargs)
2742 2742
 
  2743
+    def get_pk_dict(self):
  2744
+        return {self._meta.pk_name: self.get_pk()}
  2745
+
2743 2746
     def get_pk(self):
2744 2747
         return getattr(self, self._meta.pk_name, None)
2745 2748
 
5  tests.py
@@ -1151,6 +1151,11 @@ def test_prepared_hook(self):
1151 1151
         for e in Entry.raw('select * from entry'):
1152 1152
             self.assertTrue(e._prepared)
1153 1153
 
  1154
+    def test_get_pks(self):
  1155
+        b = self.create_blog(title='b1')
  1156
+        self.assertEqual(b.get_pk_dict(), {'id': b.get_pk()})
  1157
+        self.assertFalse(b.get_pk_dict()['id'] == None)
  1158
+
1154 1159
 
1155 1160
 class UnicodeFieldTests(BaseModelTestCase):
1156 1161
     def get_common_objects(self):

0 notes on commit 04dace3

Please sign in to comment.
Something went wrong with that request. Please try again.