Reputation: 3811
I've looked all around the documentation and internet, but I can't find an answer for this. I have a model with several fields that have (blank=True, null=True). When I save the ModelForm, the columns get set as empty strings in the database. I would like it to save them as NULL in the database (if they are empty).
How can I tell ModelForm to save empty values as NULL?
Upvotes: 0
Views: 474
Reputation: 77129
This behavior is by design. Django would rather have empty strings than NULLs, for various reasons. Google will tell you, but long story short, Django finds NULL
and ""
overlapping in meaning, and does away with the former.
What you can do, is intercept the value coming from the database driver and change empty strings to NULL
and the other way round. Implementing a custom CharField
subclass will get you there.
In doing so, you'll experience the ambiguity between NULL
and ""
for yourself :).
Upvotes: 3