Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Model Field Default to Null

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?

like image 754
SPoage Avatar asked Jan 05 '11 13:01

SPoage


People also ask

Is NULL false by default Django?

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.

What is default model Django?

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).

What is blank true in Django?

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.

What is AutoField in Django?

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.


1 Answers

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

like image 75
gruszczy Avatar answered Sep 30 '22 20:09

gruszczy