Reputation: 550
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?
Upvotes: 4
Views: 9161
Reputation: 309099
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
).
Upvotes: 8