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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With