Django problem updating date field when updating a record

I have a problem when I try to update a field named date_updated. My intention with the field is that every time a record is updated, the date_updated field of that record should be updated by the date the change is made. That field and one other field I have inside a Base class and then in each of the models I inherit that class to repeat the fields.

class Base(models.Model):
    ...
    date_updated = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        self.date_updated = django.timezone.now()
        super(Base, self).save(*args, **kwargs)

    class Meta:
        abstract = True


class OtherClass(Base):
    ...

My intention is that when I update any record in the OtherClass, its date_updated field will be updated.

I also tried adding the overwrite of the save method in the OtherClass, but the result is the same. The date is kept after I make the change.

I am making the change with .update(**data_to_update)

I did this when i wanted to update only the updated_on (datetime) column: This might help you:

from datetime import datetime

def get_current_datetime_str():
    now = datetime.now()
    return now.strftime("%Y-%m-%d %H:%M:%S")
    
class ModelName(models.Model):
    date_updated=models.CharField(max_length=100) #whatever your field is
    ...
    def __str__(self):
        return self.name

continue and write this below def str(self):

def save(self, *args, **kwargs):
    self.date_updated = get_current_datetime_str()
    super().save(*args, **kwargs)
Back to Top