Skip to content

Commit

Permalink
Fixes #100, allow None to get sent to the database regardless of …
Browse files Browse the repository at this point in the history
…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
coleifer committed Jul 11, 2012
1 parent 5532bba commit ce738d6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion peewee.py
Expand Up @@ -2241,7 +2241,7 @@ def render_field_template(self, quote_char=''):
return self.field_template % params

def db_value(self, value):
if (self.null and value is None):
if value is None:
return None
return self.column.db_value(value)

Expand Down
6 changes: 3 additions & 3 deletions tests.py
Expand Up @@ -66,7 +66,7 @@ def __unicode__(self):
class Entry(TestModel):
pk = PrimaryKeyField()
title = CharField(max_length=50, verbose_name='Wacky title')
content = TextField()
content = TextField(default='')
pub_date = DateTimeField(null=True)
blog = ForeignKeyField(Blog, cascade=True)

Expand Down Expand Up @@ -97,7 +97,7 @@ class EntryTwo(Entry):
class User(TestModel):
username = CharField(max_length=50)
blog = ForeignKeyField(Blog, null=True)
active = BooleanField(db_index=True)
active = BooleanField(db_index=True, default=False)

class Meta:
db_table = 'users'
Expand Down Expand Up @@ -2138,7 +2138,7 @@ def test_select_related_multiple(self):
# didn't select title/content/pub_date on entries, so make sure they're none
for r in results:
self.assertEqual(r.entry.title, None)
self.assertEqual(r.entry.content, None)
self.assertEqual(r.entry.content, '') # <-- default value
self.assertEqual(r.entry.pub_date, None)
self.assertFalse(r.entry.pk == None)

Expand Down

0 comments on commit ce738d6

Please sign in to comment.