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?
Field.null If True , Django will store empty values as NULL in the database. Default is False . Note that empty string values will always get stored as empty strings, not as NULL . Only use null=True for non-string fields such as integers, booleans and dates.
default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. null: If True , Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string).
Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the DB. blank determines whether the field will be required in forms. This includes the admin and your custom forms. If blank=True then the field will not be required, whereas if it's False the field cannot be blank.
AutoField. An IntegerField that automatically increments according to available IDs. You usually won't need to use this directly; a primary key field will automatically be added to your model if you don't specify otherwise.
Try default=None
. There is no NULL
in python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With