Skip to content

Instantly share code, notes, and snippets.

@sprin
Created August 30, 2012 23:55
Show Gist options
  • Save sprin/3545676 to your computer and use it in GitHub Desktop.
Save sprin/3545676 to your computer and use it in GitHub Desktop.
Django: CharField that will save an empty string as NULL
class CharFieldNoEmptyString(CharField):
def get_prep_value(self, value):
# Cast empty strings to null before saving to the DB.
value = (
super(CharFieldNoEmptyString, self)
.get_prep_value(value)
)
return value or None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment