Irfan Harun
Irfan Harun

Reputation: 1059

default date and time for DateTimeField in django

in my models, I have :

date_time = models.DateTimeField(auto_now = True)

This is surely works and adds current date and time for new records that get created.

However, I want to set the default date and time for existing records as 01/01/2020 00:00:01.

if I try to add a default along with auto_now, I get an error stating auto_now and default are mutually exclusive.

Upvotes: 0

Views: 151

Answers (1)

AKX
AKX

Reputation: 168834

If you're adding this field now, you'll need two migration steps.

First, add

date_time = models.DateTimeField()

and run makemigrations. Django will ask you what to use for the default; tell it you'll want datetime.datetime(2020, 1, 1, 0, 0, 1).

Then modify the field to

date_time = models.DateTimeField(auto_now=True)

and run makemigrations again.

Upvotes: 1

Related Questions