danijar
danijar

Reputation: 34185

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.

Upvotes: 7

Views: 2073

Answers (1)

freakish
freakish

Reputation: 56477

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.

Upvotes: 4

Related Questions