Skip to content
This repository
Browse code

InsertQuery will respect default field values on model instances when

those values are not specified explicitly -- fixes #107
  • Loading branch information...
commit c102866375f9b8ad823939f92051137a766ec5e8 1 parent 506ecff
Charles Leifer authored September 09, 2012

Showing 2 changed files with 21 additions and 6 deletions. Show diff stats Hide diff stats

  1. 20  peewee.py
  2. 7  tests.py
20  peewee.py
@@ -1805,7 +1805,9 @@ def execute(self):
1805 1805
 
1806 1806
 class InsertQuery(BaseQuery):
1807 1807
     def __init__(self, _model, **kwargs):
1808  
-        self.insert_query = kwargs
  1808
+        query = _model._meta.get_default_dict()
  1809
+        query.update(kwargs)
  1810
+        self.insert_query = query
1809 1811
         super(InsertQuery, self).__init__(_model)
1810 1812
 
1811 1813
     def parse_insert(self):
@@ -2511,6 +2513,15 @@ def prepared(self):
2511 2513
             if field.default is not None:
2512 2514
                 self.defaults[field.name] = field.default
2513 2515
 
  2516
+    def get_default_dict(self):
  2517
+        dd = {}
  2518
+        for field_name, default in self.defaults.items():
  2519
+            if callable(default):
  2520
+                dd[field_name] = default()
  2521
+            else:
  2522
+                dd[field_name] = default
  2523
+        return dd
  2524
+
2514 2525
     def get_sorted_fields(self):
2515 2526
         return sorted(self.fields.items(), key=lambda (k,v): (k == self.pk_name and 1 or 2, v._order))
2516 2527
 
@@ -2654,11 +2665,8 @@ def __init__(self, *args, **kwargs):
2654 2665
             setattr(self, k, v)
2655 2666
 
2656 2667
     def initialize_defaults(self):
2657  
-        for field_name, default in self._meta.defaults.items():
2658  
-            if callable(default):
2659  
-                val = default()
2660  
-            else:
2661  
-                val = default
  2668
+        dd = self._meta.get_default_dict()
  2669
+        for field_name, val in dd.items():
2662 2670
             setattr(self, field_name, val)
2663 2671
 
2664 2672
     def prepared(self):
7  tests.py
@@ -654,6 +654,13 @@ def test_insert(self):
654 654
         self.assertSQLEqual(iq.sql(), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']))
655 655
         self.assertEqual(iq.execute(), 2)
656 656
 
  657
+        # test inserting with defaults, references #107
  658
+        iq = User.insert(username='foo')
  659
+        self.assertSQLEqual(iq.sql(), ('INSERT INTO `users` (`active`,`username`) VALUES (?,?)', [False, 'foo']))
  660
+
  661
+        iq = InsertQuery(User, username='foo')
  662
+        self.assertSQLEqual(iq.sql(), ('INSERT INTO `users` (`active`,`username`) VALUES (?,?)', [False, 'foo']))
  663
+
657 664
     def test_update(self):
658 665
         iq = InsertQuery(Blog, title='a').execute()
659 666
 

0 notes on commit c102866

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