Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

postgresql - Django model: NULLable field

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 !

question from:https://stackoverflow.com/questions/16046478/django-model-nullable-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...