Ching Chong
Ching Chong

Reputation: 723

how to edit the date time field only the first time

I have a object in a model

date_created = models.DateTimeField(auto_now=True)

Now this saves the current date and time when the entry has been entered into the database, as well as changes the time when the entry has been edited.

Is there any way such that the date and time is changed only when its first entered into the database and not when its edited?

Upvotes: 2

Views: 217

Answers (1)

Dominic Rodger
Dominic Rodger

Reputation: 99771

Use auto_now_add rather than auto_add.

auto_now_add is used to:

[automatically] set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it's not just a default value that you can override.

Whereas auto_now is to:

[automatically] set the field to now every time the object is saved. Useful for "last-modified" timestamps. Note that the current date is always used; it's not just a default value that you can override.

Upvotes: 4

Related Questions