I am adding the date_added and date_modified fields to a bunch of generic models in my current project. I will subclass models. Modeling and adding appropriate fields, but I want to add automatic saving of behavior (i.e. time evey, when someone calls MyModel.save (), the date_modified field is updated. I see two approaches: overriding the save () method or adding a pre_save signal handler in an abstract base class.
class CommonData(models.Model): date_added = models.DateTimeField(default=datetime.datetime.today,null=False,blank=False) date_modified = models.DateTimeField(default=datetime.datetime.today,null=True,blank=True)
or
class CommonData(models.Model): date_added = models.DateTimeField(default=datetime.datetime.today,null=False,blank=False) date_modified = models.DateTimeField(default=datetime.datetime.today,null=True,blank=True)
I'm new to Django and Python and wondering which approach was more "django"? Which is more efficient? which is the "right" way to do this?
Deano source share