Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does null=True imply default=None for models?

When allowing database fields of a Django model to be NULL using null=True, is there default value guaranteed to be NULL by default? Or do I have to speficy this manually:

report = models.FileField(upload_to='reports', null=True, default=None)
                                                          ^^^^^^^^^^^^

I couldn't found anything about it in the documentation about models fields.

like image 707
danijar Avatar asked Oct 02 '22 04:10

danijar


1 Answers

Not quite:

django/db/models/fields/__init__.py

class NOT_PROVIDED:
    pass

class Field(RegisterLookupMixin):
    # some code
    def __init__(..., default=NOT_PROVIDED, ...):

which basically means it is whatever it is in database. For example if you create your table and you set default value directly in database (without altering models), then it will use the value from the database.

like image 98
freakish Avatar answered Oct 03 '22 18:10

freakish