In django do models have a default timestamp field?

前端 未结 6 1380
误落风尘
误落风尘 2021-01-30 09:53

In django - is there a default timestamp field for all objects? That is, do I have to explicitly declare a \'timestamp\' field for \'created on\' in my Model - or is there a way

6条回答
  •  半阙折子戏
    2021-01-30 10:31

    Automagically doesn't sound like something django would do by default. It wouldn't force you to require a timestamp.

    I'd build an abstract base class and inherit all models from it if you don't want to forget about the timestamp / fieldname, etc.

    class TimeStampedModel(models.Model):
         created_on = models.DateTimeField(auto_now_add=True)
    
         class Meta:
             abstract = True
    

    It doesn't seem like much to import wherever.TimeStampedModel instead of django.db.models.Model

    class MyFutureModels(TimeStampedModel):
        ....
    

提交回复
热议问题