Skip to content
This repository
Browse code

Fixes #100, allow ``None`` to get sent to the database regardless of …

…whether the

field in question is nullable.  This prevents the None from getting cast
to a value appropriate to the field and will result in IntegrityErrors.
This is *more* desirable than non-null values getting stored
incorrectly.  Especial thanks to @mkosler for being insistent on this.
  • Loading branch information...
commit ce738d6dd89edc7d9850c368a91cd9f063f5a3a5 1 parent 5532bba
Charles Leifer authored July 10, 2012

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

  1. 2  peewee.py
  2. 6  tests.py
2  peewee.py
@@ -2241,7 +2241,7 @@ def render_field_template(self, quote_char=''):
2241 2241
         return self.field_template % params
2242 2242
 
2243 2243
     def db_value(self, value):
2244  
-        if (self.null and value is None):
  2244
+        if value is None:
2245 2245
             return None
2246 2246
         return self.column.db_value(value)
2247 2247
 
6  tests.py
@@ -66,7 +66,7 @@ def __unicode__(self):
66 66
 class Entry(TestModel):
67 67
     pk = PrimaryKeyField()
68 68
     title = CharField(max_length=50, verbose_name='Wacky title')
69  
-    content = TextField()
  69
+    content = TextField(default='')
70 70
     pub_date = DateTimeField(null=True)
71 71
     blog = ForeignKeyField(Blog, cascade=True)
72 72
 
@@ -97,7 +97,7 @@ class EntryTwo(Entry):
97 97
 class User(TestModel):
98 98
     username = CharField(max_length=50)
99 99
     blog = ForeignKeyField(Blog, null=True)
100  
-    active = BooleanField(db_index=True)
  100
+    active = BooleanField(db_index=True, default=False)
101 101
 
102 102
     class Meta:
103 103
         db_table = 'users'
@@ -2138,7 +2138,7 @@ def test_select_related_multiple(self):
2138 2138
         # didn't select title/content/pub_date on entries, so make sure they're none
2139 2139
         for r in results:
2140 2140
             self.assertEqual(r.entry.title, None)
2141  
-            self.assertEqual(r.entry.content, None)
  2141
+            self.assertEqual(r.entry.content, '') # <-- default value
2142 2142
             self.assertEqual(r.entry.pub_date, None)
2143 2143
             self.assertFalse(r.entry.pk == None)
2144 2144
 

0 notes on commit ce738d6

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