Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DateTimeField Defined as blank=True, null=True but doesn't allow null

Tags:

python

django

I have a DateTimeField in one of my Django models.

    completed_date = models.DateTimeField('date completed', blank=True, null=True)

I've defined it to allow blank and null values. However, when I try to create an instance of the model, I get the following error:

IntegrityError at /admin/tasks/project/add/

tasks_project.completed_date may not be NULL

I'm using Django 1.25 and Python 2.7. Anyone know why this is happening? Is there anything I can do to fix this?

I found a ticket that describes the same problem, but it was closed as fixed 4 years ago, so I assume it must have been integrated into Django by now!

like image 277
Mark Nenadov Avatar asked Mar 20 '11 12:03

Mark Nenadov


People also ask

What is the difference between NULL true and blank true in Django?

null=True will set the field's value to NULL i.e., no data. It is basically for the databases column value. blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms.

How do I allow NULL values in Django model?

The Django convention is to use the empty string, not NULL. The default values of null and blank are False. Also there is a special case, when you need to accept NULL values for a BooleanField , use NullBooleanField instead.

How does Django handle NULL value?

If a string-based field has null=True , that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it's redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

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.


1 Answers

django syncdb and an updated model

from that question/answer:

Django doesn't support migrations out of the box. There is a pluggable app for Django that does exactly that though, and it works great. It's called South.

http://south.aeracode.org/

Havent used django in a while, but i seem to remember that syncdb does perform alter commands on db tables. you have to drop the table then run again and it will create again.

like image 125
dting Avatar answered Sep 28 '22 19:09

dting