[Django]-Can not assign None to Django DateTimeField()

8👍

You misunderstand what auto_now does

Automatically set the field to now every time the object is saved.

You’ll still need to specify a valid default or allow for null values with null=True

👤Sayse

2👍

You cannot assign None to updated_datetime because you have not set null=True in the model.

You have created a migration that tells django what to do if it encounters existing rows in the circuit table when adding the field but that is not the same as being able to do it.

The default value asked for by the migration does not check that value is allowed to be used, it’s trusting you to know what you are doing.

In your migration use datetime.now() rather than None.

👤Tony

1👍

DateField.auto_now

Automatically set the field to now every time the object is saved.

DateField.auto_now_add

Automatically set the field to now when the object is first created.

You can use anyone:

created_date = models.DateTimeField(auto_now_add=True, null=True)

or

created_date = models.DateTimeField(null=True)

or

from django.utils import timezone
created_date = models.DateTimeField(default=timezone.now)
👤Khabir

0👍

  1. You have to delete all migrations and erase your database data too and do migrations again

  2. excute commands:

python manage.py makemigrations
python manage.py migrate --fake
👤Ershan

Leave a comment