Django Timestamped Model

Django -- Posted on Sept. 28, 2017

The code you provided defines an abstract model class called Timestamped, which contains two fields, created and modified, to keep track of the creation and modification timestamps of any model that inherits from it.

Here's a breakdown of the code:

  1. models.Model: This indicates that the Timestamped class is a Django model.

  2. created: This is a DateTimeField with the option auto_now_add=True, which means that it will be automatically set to the current date and time when an instance of the model is created for the first time. It will not be updated when the model instance is modified.

  3. modified: This is also a DateTimeField with the option auto_now=True, which means that it will be automatically updated to the current date and time every time the model instance is saved or modified.

  4. class Meta: This is a class that contains metadata options for the model. In this case, abstract = True is used, indicating that Timestamped is an abstract model. Abstract models are meant to be used as a base class and are not intended to be instantiated directly. Instead, other models can inherit from this abstract class to share the same fields and behavior.

 

              
                class Timestamped(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True
                  
   
            

Related Posts