在 django 中,模型有默认时间戳字段吗?

发布于 2024-12-13 19:09:36 字数 85 浏览 0 评论 0原文

在 django 中 - 所有对象都有默认的时间戳字段吗?也就是说,我是否必须在模型中显式声明“创建于”的“时间戳”字段 - 或者有没有办法自动获取此字段?

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 to get this automagically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜味拾荒者 2024-12-20 19:09:36

默认情况下没有这样的东西,但添加一个非常容易。只需使用 DateTimeField 类中的 auto_now_add 参数即可:

created = models.DateTimeField(auto_now_add=True)

您还可以将 auto_now 用于“更新日期”字段。
检查 auto_now 此处

对于 auto_now_add 这里

具有这两个字段的模型将如下所示:

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

No such thing by default, but adding one is super-easy. Just use the auto_now_add parameter in the DateTimeField class:

created = models.DateTimeField(auto_now_add=True)

You can also use auto_now for an 'updated on' field.
Check the behavior of auto_now here.

For auto_now_add here.

A model with both fields will look like this:

class MyModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
笑着哭最痛 2024-12-20 19:09:36

Automagically 听起来不像 django 默认情况下会做的事情。它不会强迫您需要时间戳。

如果您不想忘记时间戳/字段名等,我会构建一个抽象基类并从中继承所有模型。

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

     class Meta:
         abstract = True

导入 wherever.TimeStampedModel 似乎并不多django.db.models.Model 的

class MyFutureModels(TimeStampedModel):
    ....

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):
    ....
岁月静好 2024-12-20 19:09:36

如果您使用 django-extensions (这是一个很好的应用程序,可以向django-admin.py 命令行助手)您可以通过继承 他们的 TimeStampedModel 或使用他们的 自定义时间戳字段

If you are using django-extensions (which is a good app for adding functionality to the django-admin.py command line helper) you can get these model fields for free by inheriting from their TimeStampedModel or using their custom TimeStamp fields

喜你已久 2024-12-20 19:09:36

你可以尝试 django-extensions

如果你想使用时间戳抽象模型,

from django_extensions.db.models import TimeStampedModel

class A(TimeStampedModel):
   ...

它还有其他抽象模型。你也可以用它。

You can try django-extensions

if you want to use time-stamp abstract model

from django_extensions.db.models import TimeStampedModel

class A(TimeStampedModel):
   ...

It has other abstract models. you can use that too.

眉黛浅 2024-12-20 19:09:36

如果您希望能够修改此字段,请设置以下内容而不是 auto_now_add=True:

For Date

from datetime import date

models.DateField(default=date.today)

For DateTime

from django.utils import timezone

models.DateTimeField(default=timezone.now)

If you want to be able to modify this field, set the following instead of auto_now_add=True:

For Date

from datetime import date

models.DateField(default=date.today)

For DateTime

from django.utils import timezone

models.DateTimeField(default=timezone.now)
落在眉间の轻吻 2024-12-20 19:09:36

由于您可能在模型中使用 ID,因此以这种方式使用它们是安全的。

from django.db import models
from uuid import uuid4


class TimeStampedModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

然后将 TimeStampedModel 导入到您想要使用它们的任何模型中,例如

class Detail(TimeStampedModel):
   first_name = models.CharField(max_length=100)
   last_name = models.CharField(max_length=100)

   class Meta:
       verbose_name = "Detail"
       verbose_name_plural = "Details"

Since you might be using IDs with your models, it's safe to use them this way.

from django.db import models
from uuid import uuid4


class TimeStampedModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Then import the TimeStampedModel in any model you want to use them, eg

class Detail(TimeStampedModel):
   first_name = models.CharField(max_length=100)
   last_name = models.CharField(max_length=100)

   class Meta:
       verbose_name = "Detail"
       verbose_name_plural = "Details"
不及他 2024-12-20 19:09:36

我想我会同意

created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

我需要一些关于使用的澄清

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

    class Meta:
         abstract = True

I think i would go with

created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

I need some clarifications on using

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

    class Meta:
         abstract = True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文