I have a Django model which looks like this:
parent = models.ForeignKey(ParentModel)
name = models.CharField(blank=True, max_length=200)
... other fields ...
unique_together = ("name", "parent")
Firstly, blank (empty string) IS NOT same as null (
'' != None).
Secondly, Django CharField when used through forms will be storing empty string when you leave field empty.
So if your field was something else than CharField you should just add
null=True to it. But in this case you need to do more than that. You need to create subclass of
forms.CharField and override it's
clean method to return None on empty string, something like this:
class NullCharField(forms.CharField): def clean(self, value): value = super(NullCharField, self).clean(value) if value in forms.fields.EMPTY_VALUES: return None return value
and then use it in form for your ModelForm:
class MyModelForm(forms.ModelForm): name = NullCharField(required=False, ...)
this way if you leave it blank it will store null in database instead of empty string (