Unique fields that allow nulls in Django Unique fields that allow nulls in Django django django

Unique fields that allow nulls in Django


Django has not considered NULL to be equal to NULL for the purpose of uniqueness checks since ticket #9039 was fixed, see:

http://code.djangoproject.com/ticket/9039

The issue here is that the normalized "blank" value for a form CharField is an empty string, not None. So if you leave the field blank, you get an empty string, not NULL, stored in the DB. Empty strings are equal to empty strings for uniqueness checks, under both Django and database rules.

You can force the admin interface to store NULL for an empty string by providing your own customized model form for Foo with a clean_bar method that turns the empty string into None:

class FooForm(forms.ModelForm):    class Meta:        model = Foo    def clean_bar(self):        return self.cleaned_data['bar'] or Noneclass FooAdmin(admin.ModelAdmin):    form = FooForm


** edit 11/30/2015: In python 3, the module-global __metaclass__ variable is no longer supported.Additionaly, as of Django 1.10 the SubfieldBase class was deprecated:

from the docs:

django.db.models.fields.subclassing.SubfieldBase has been deprecated and will be removed in Django 1.10. Historically, it was used to handle fields where type conversion was needed when loading from the database, but it was not used in .values() calls or in aggregates. It has been replaced with from_db_value(). Note that the new approach does not call the to_python() method on assignment as was the case with SubfieldBase.

Therefore, as suggested by the from_db_value() documentation and this example, this solution must be changed to:

class CharNullField(models.CharField):    """    Subclass of the CharField that allows empty strings to be stored as NULL.    """    description = "CharField that stores NULL but returns ''."    def from_db_value(self, value, expression, connection, contex):        """        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 ''        # Otherwise, just return the value.        return value    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

I think a better way than overriding the cleaned_data in the admin would be to subclass the charfield - this way no matter what form accesses the field, it will "just work." You can catch the '' just before it is sent to the database, and catch the NULL just after it comes out of the database, and the rest of Django won't know/care. A quick and dirty example:

from django.db import modelsclass CharNullField(models.CharField):  # subclass the CharField    description = "CharField that stores NULL but returns ''"    __metaclass__ = models.SubfieldBase  # this ensures to_python will be called    def to_python(self, value):        # this is the value right out of the db, or an instance        # if an instance, just return the instance        if isinstance(value, models.CharField):            return value         if value is None:  # if the db has a NULL (None in Python)            return ''      # convert it into an empty string        else:            return value   # otherwise, just return the value    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  

For my project, I dumped this into an extras.py file that lives in the root of my site, then I can just from mysite.extras import CharNullField in my app's models.py file. The field acts just like a CharField - just remember to set blank=True, null=True when declaring the field, or otherwise Django will throw a validation error (field required) or create a db column that doesn't accept NULL.


Because I am new to stackoverflow I am not yet allowed to reply to answers, but I would like to point out that from a philosophical point of view, I can't agree with the most popular answer tot this question. (by Karen Tracey)

The OP requires his bar field to be unique if it has a value, and null otherwise. Then it must be that the model itself makes sure this is the case. It cannot be left to external code to check this, because that would mean it can be bypassed. (Or you can forget to check it if you write a new view in the future)

Therefore, to keep your code truly OOP, you must use an internal method of your Foo model. Modifying the save() method or the field are good options, but using a form to do this most certainly isn't.

Personally I prefer using the CharNullField suggested, for portability to models I might define in the future.