I am a newbie of Django. This is the problem I have encountered.
models.py:
created_time = models.DateTimeField('Created Time', auto_now_add=True)
When I migrations:
Then, I add the default to it:
created_time = models.DateTimeField('Created Time', auto_now_add=True, default=timezone.now)
I migrations it again:
So, can somebody tell me how to use DateTimeField with auto_now_add=True?
As the error says, you can't set auto_now_add=True
and specify a default at the same time.
The problem is that Django needs to know what value to use for your existing entries in the database.
You can either set null=True
, then the value will be left as None
.
created_time = models.DateTimeField('Created Time', auto_now_add=True, null=True)
Or, simply remove the default, and run makemigrations again.
created_time = models.DateTimeField('Created Time', auto_now_add=True)
When Django prompts you, select Option 1), and specify a default (e.g. timezone.now
).
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