[Django]-Django DateTimeField with auto_now_add asks for default

33๐Ÿ‘

โœ…

You have already some rows without created_at value.
But when you added following field and run migration

created_at = models.DateTimeField( auto_now_add = True)

It expects the existing row has created_at value. So, It warns you at the time of migrations.

To solve this problem.
You can choose the 1st option which says Provide a one-off default now (will be set on all existing rows).
By choosing the 1st option you are asking for value. Here you can enter timezone.now(). It will populate existing rows with the current timestamp.

๐Ÿ‘คshafikshaon

9๐Ÿ‘

Delete the migration file in app and you can makemigrtion process at the time no error raises, It raises due to the reason of previously created time object.

๐Ÿ‘คSeetharam

1๐Ÿ‘

if you are using a sqlite database you can omit it and again make it,
or you can write null=True as an argument for your DateTimeFeild()
i suggest you the second one that i have done before

๐Ÿ‘คAli Eyvazi

1๐Ÿ‘

you can create two migrations,

  • the first one sets a default value,
  • the second remove the default and add the auto_now_add
๐Ÿ‘คIDM

0๐Ÿ‘

That is because auto_now_add = True make django determine every instance of the model should have a value instead of null.

๐Ÿ‘คBoyce Cecil

0๐Ÿ‘

when migrating use:

  1. python manage.py makemigrations
  2. python manage.py migrate โ€“run-syncdb
๐Ÿ‘คatp55

Leave a comment