[Django]-Updated at field in django model

60๐Ÿ‘

โœ…

I would just have 2 fields on the model, one for created and one that records updated time like this

class Location(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

If you are using django-model-utils you can subclass the TimeStampedModel, which has both created and modified fields.

#Django model utils TimeStampedModel
class TimeStampedModel(models.Model):
    """
    An abstract base class model that provides self-updating
    ``created`` and ``modified`` fields.

    """
    created = AutoCreatedField(_('created'))
    modified = AutoLastModifiedField(_('modified'))

    class Meta:
        abstract = True

class Location(TimeStampedModel):
    """
    Add additional fields
    """
๐Ÿ‘คPieter Hamman

Leave a comment