13

I find that when I insert nothing of a string form field through a form it captures it as an empty string ''.

However when I insert nothing of an integer form field through a form it captures it as [null].

Is this good practice? Should the string be null as well in the db?

zentenk
  • 2,725
  • 6
  • 25
  • 31

3 Answers3

7

It is good practice. If strings were allowed to be null, there would be two ways to have an empty string: null and "". This gives you multiple values to check against, which is inefficient and messy.

This is a Django convention, but is good practice in all applications, unless you have the need for null to mean something different than "".

Dave
  • 11,499
  • 5
  • 34
  • 46
  • 13
    If in my database I set a field to ALLOW NULL, then I expect None to be stored as NULL, not an empty string. It's fine for Django to have it's conventions, but Django is not necessarily the only system accessing the database. If a separate application can store NULL then you end up having to still check for null vs empty string. However a simple `if not value` will check for both in Python. – six8 Oct 08 '12 at 21:31
  • 20
    It might be 'django practice' but in databases in general using the empty string to represent missing data is bad practice. That's what NULL is for. I really wish this wasn't standard practice in django. – Darren Apr 19 '16 at 09:04
  • 4
    It's not a good practice it's a convention introduced in early days, for not having to handle `None` in templates and not having to clean blank form fields into `None`. It was a shortcut to get things done quickly, but we should justify it as a good practice now, because it was laziness or lack of time that got us this "practice". That's why we now have 2 field attributes handling this stuff. – Janusz Skonieczny Aug 08 '17 at 11:10
  • I just manually set the Charfield(default=None) so that if its nullable it will be set as NULL instead of empty string, or if non-nullable, will raise IntegrityError on DB level – Finn Andersen Nov 11 '21 at 08:08
3

It depends on the field.empty_strings_allowed attribute.

Some fields have it overridden to False. But CharField keeps it True, thus making Field._get_default return empty string as default — even if blank=True. The only way to get None as default is to have null=True on a field.

So if you have field in db that should never be null and not allow blanks to be never ever put in there by for e.g. objects.create. You need to put self.full_clean() — or other validation — in the model save method.

Janusz Skonieczny
  • 17,642
  • 11
  • 55
  • 63
  • We had to create a custom Field in our project just to set empty_strings_allowed to False. Thanks for the answer and this behavior of the CharField doesn't really make any sense to me. Event when you have a Charfield as primary_key=True it just lets you create empty primary keys. – gurel_kaynak Dec 03 '21 at 07:21
2

I think the string should be NULL too, but depends on your application see MySQL, better to insert NULL or empty string?.

If you want to store NULLs for an empty field, add null=True to the Field in question. See https://docs.djangoproject.com/en/dev/ref/models/fields/#null

Community
  • 1
  • 1
six8
  • 2,886
  • 21
  • 20
  • 3
    I prefer to know if the value was set, even if I set it to an empty string vs not set (null). This of course depends on your preference and/or application. In any case, I gave my opinion of best practice and gave instructions on how to enable it with Django. – six8 Oct 08 '12 at 21:33
  • 2
    @Cixate: It is incorrect, at least in the case of strings because Django will always store blank fields as empty strings, even if you specify `null=True`. That is Django convention. – Beau Jun 12 '14 at 15:31