Coding_line
Coding_line

Reputation: 127

How to automatically update the date field through Django Class model

I am having 2 fields in database named Created_date and updated_date in table and also defined it in django model class and also i am using the default admin console with which user can come and insert or update records.

But i want the above two fields to be inserted automatically and not by inserting or updating through default admin console.

For created_date:It should take the value when the record has been newly inserted and wont change if any of the fields is getting updated.

For Updated_date:It should take the current date whenever record is getting inserted or updated.

Also i want this two fields to be displayable but uneditable..Can i define it through property editable='False'

Regards

Upvotes: 2

Views: 5716

Answers (1)

Todor
Todor

Reputation: 16010

You need to use the auto_now=False and auto_now_add=False field options coming with DateField/DateTimeField

date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)

Upvotes: 7

Related Questions