We have a Model that includes an auto_add_now field and have decided that we need to be able to occasionally update it manually. However, in writing the DRF serializer / view it's unclear how best to handle this, since it's normally not editable we need to make a change. We're considering using the pre-save trick (AutoDateTimeField): Django auto_now and auto_now_add but that answer is 9 years old.
class AutoDateTimeField(models.DateTimeField):
def pre_save(self, model_instance, add):
return timezone.now()
#usage
created_at = models.DateField(default=timezone.now)
updated_at = models.AutoDateTimeField(default=timezone.now)
Are there any downsides? This is in an abstract base class which affects many models. Is there a better way (Django 1.11.7)?