Django Model Field Default to Null

PythonDjangoDjango Models

Python Problem Overview


I need to have my Django application allow me to have a default value of NULL set for a certain model field. I've looked over the null, blank, and default parameters, but it's not very clear what combination of the three I need to use to get the desired effect. I've tried setting default=NULL but it threw an error. If I specify blank=True, null=True and no default, will it just default back to NULL come runtime?

Python Solutions


Solution 1 - Python

Try default=None. There is no NULL in python.

Solution 2 - Python

If you specify null=True on the model field then the value will be stored as NULL in the database if the user does not provide a value.

Solution 3 - Python

  • blank=True allows you to input nothing (i.e "", None) and keep it empty.
  • null=True means the database row is allowed to be NULL.
  • default=None sets the field to None if no other value is given.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSPoageView Question on Stackoverflow
Solution 1 - PythongruszczyView Answer on Stackoverflow
Solution 2 - PythonKevinView Answer on Stackoverflow
Solution 3 - PythonHenning LeeView Answer on Stackoverflow