Django model: NULLable field

DjangoPostgresqlModelNull

Django Problem Overview


I'm working on a django project where I need a DateField to sometimes be empty. My model looks like this:

#models.py
end = models.DateField(default=None, blank=True)

But when I run python manage.py sql myapp the sql statement always end up being

CREATE TABLE "myapp_date" (
"end" date NOT NULL
);

Therefore my field isn't nullable and I can't understand what I should do to make it so. Any idea would be appreciated !

Django Solutions


Solution 1 - Django

You should use

end = models.DateField(default=None, blank=True, null=True)

Basically blank allows you to pass it a null value, but null tells the database to accept null values.

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
QuestionRobinView Question on Stackoverflow
Solution 1 - DjangoNgenatorView Answer on Stackoverflow