Django模型表单对象的自动创建日期?

问题:Django模型表单对象的自动创建日期?

自动设置对象创建日期以及记录该对象上次更新时间的字段的最佳方法是什么?

models.py:

created_at = models.DateTimeField(False, True, editable=False)
updated_at = models.DateTimeField(True, True, editable=False)

views.py:

if request.method == 'POST':
    form = MyForm(request.POST)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.user = request.user
        obj.save()
        return HttpResponseRedirect('obj_list')

我得到错误:

objects_object.created_at may not be NULL

我是否必须自己手动设置此值?我以为那是传递给参数的重点DateTimeField(或者它们只是默认值,由于我已经设置了editable=False它们,所以它们不会在表单上显示,因此不会在请求中提交,因此也不会得到放入表格?)。

最好的方法是什么?一种__init__方法是什么?

What’s the best way to set a creation date for an object automatically, and also a field that will record when the object was last updated?

models.py:

created_at = models.DateTimeField(False, True, editable=False)
updated_at = models.DateTimeField(True, True, editable=False)

views.py:

if request.method == 'POST':
    form = MyForm(request.POST)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.user = request.user
        obj.save()
        return HttpResponseRedirect('obj_list')

I get the error:

objects_object.created_at may not be NULL

Do I have to manually set this value myself? I thought that was the point of the parameters passed to DateTimeField (or are they just defaults, and since I’ve set editable=False they don’t get displayed on the form, hence don’t get submitted in the request, and therefore don’t get put into the form?).

What’s the best way of doing this? An __init__ method?


回答 0

您可以分别为和使用auto_nowauto_now_add选项。updated_atcreated_at

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

You can use the auto_now and auto_now_add options for updated_at and created_at respectively.

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

回答 1

好吧,上面的答案是正确的,auto_now_addauto_now可以做到,但是最好创建一个抽象类,并在需要created_atupdated_at字段的任何模型中使用它。

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

    class Meta:
        abstract = True

现在,您可以在任何要使用它的地方进行简单的继承,并且可以在任何喜欢的模型中使用时间戳。

class Posts(TimeStampMixin):
    name = models.CharField(max_length=50)
    ...
    ...

这样,您可以在Django DRY中利用面向对象的可重用性(不要重复自己)

Well, the above answer is correct, auto_now_add and auto_now would do it, but it would be better to make an abstract class and use it in any model where you require created_at and updated_at fields.

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

    class Meta:
        abstract = True

Now anywhere you want to use it you can do a simple inherit and you can use timestamp in any model you make like.

class Posts(TimeStampMixin):
    name = models.CharField(max_length=50)
    ...
    ...

In this way, you can leverage object-oriented reusability, in Django DRY(don’t repeat yourself)