Created
January 30, 2019 09:07
-
-
Save BrambleXu/2b886d3da7451afe1bdbb5d7cba2607f to your computer and use it in GitHub Desktop.
CharField that stores NULL but returns empty string
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
from django.db import models | |
class CharNullField(models.CharField): | |
""" | |
Subclass of the CharField that allows empty strings to be stored as NULL. | |
This class is used to set charfield be optional but unique when added. | |
Set blank=True, null=True when declaring the field | |
""" | |
description = "CharField that stores NULL but returns ''." | |
def get_prep_value(self, value): | |
""" | |
Catches value right before sending to db. | |
""" | |
if value == '': # If Django tries to save an empty string, send the db None (NULL). | |
return None | |
else: # Otherwise, just pass the value. | |
return value | |
def from_db_value(self, value, expression, connection): | |
""" | |
Gets value right out of the db and changes it if its ``None``. | |
""" | |
if value is None: | |
return '' | |
else: | |
return value | |
def to_python(self, value): | |
""" | |
Gets value right out of the db or an instance, and changes it if its ``None``. | |
""" | |
if isinstance(value, models.CharField): # If an instance, just return the instance. | |
return value | |
if value is None: # If db has NULL, convert it to ''. | |
return '' | |
return value # Otherwise, just return the value. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment