Reputation: 550
How can I set the default value as current date and time in a model?
my model is :
class StudUni(models.Model):
student_id = models.IntegerField(blank=True, null=True)
uni_name = models.CharField(max_length=55, blank=True, null=True)
last_updated = models.DateTimeField(blank=True)
Upvotes: 0
Views: 2559
Reputation: 69
last_updated = models.DateTimeField(auto_now=True)
For more info you can check here:
Upvotes: 1
Reputation: 4254
i suggest you adding two timestamps, one (date_created
) to save the date the object is created (for once) and the other one (date_updated
or last_updated
) to keep track on updates:
try this code below:
from django.utils.translation import gettext_lazy as _
[..]
class StudUni(models.Model):
student_id = models.IntegerField(blank=True, null=True)
uni_name = models.CharField(max_length=55, blank=True, null=True)
# timestamps
date_created = models.DateTimeField(_('date created'), auto_now_add=True)
last_updated = models.DateTimeField(_('last updated'), auto_now=True)
Upvotes: 0
Reputation: 581
class StudUni(models.Model):
student_id = models.IntegerField(blank=True, null=True)
uni_name = models.CharField(max_length=55, blank=True, null=True)
last_updated = models.DateTimeField(auto_now_add=True,blank=True)
This will set the current date and time whenever its saved
Upvotes: 0