Created
August 30, 2012 23:55
-
-
Save sprin/3545676 to your computer and use it in GitHub Desktop.
Django: CharField that will save an empty string as NULL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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