简体   繁体   English

Django DateField 上的默认值格式

[英]Default value format on Django DateField

I'm trying to specify a default date in a Django model, for example:我正在尝试在 Django 模型中指定默认日期,例如:

from datetime import date

class A(models.Model):
    date = models.DateField(default=date.today())

This works and I can see the default date in a ModelForm , but when I change the form input_format to %d-%m-%Y , the default date never appears in the field.这有效,我可以在ModelForm看到默认日期,但是当我将表单 input_format 更改为%d-%m-%Y ,默认日期永远不会出现在字段中。

I've also tried:我也试过:

from datetime import date

class A(models.Model):
    date = models.DateField(default=date.today().strftime('%d-%m-%Y'))

This doesn't work either.这也行不通。 Can anyone help me?谁能帮我?

There are two problems here:这里有两个问题:

  1. the DateField(default=today.today()) will not work, since then the function will be evaluated eagerly , and then the default value is thus the result of that function call.所述DateField(default=today.today())不会工作,因为则该函数将被急切地求值,然后将默认值因此是函数调用的结果。 As a result the default value is not calculated when constructing a new object, and hence eventually will be different;因此,在构造新对象时不会计算默认值,因此最终会有所不同; and
  2. the representation of a DateField . DateField的表示。 Now the model layer does not specify a representation, it only specifies how to store values in the database, and defines functions on a model.现在模型没有指定的表示中,只规定了如何在数据库中的值,并定义函数存储在一个模型。

We can solve the first problem by passing a reference to the today function, like:我们可以通过传递对today函数的引用来解决第一个问题,例如:

from datetime import date

class A(models.Model):
    date = models.DateField(default=date.today)    # no ()

As for the representation , you should specify the format in the template , for example with the date template filter [Django-doc] , like:至于表示,您应该在模板中指定格式,例如使用date模板过滤器 [Django-doc] ,例如:

<!--  template.html -->
{{ some_a.date|date:'d-m-Y' }}

Or in a form with:或采用以下形式:

# app/forms.py

class AForm(ModelForm):
    date = DateField(input_formats=['%d-%m-%Y'])
    class Meta:
       model = A

You can use the DATE_INPUT_FORMATS setting in your Django project settings, this will allow you to make date.today().strftime('%d-%m-%Y') be accepted by your model field;您可以在 Django 项目设置中使用DATE_INPUT_FORMATS设置,这将允许您使date.today().strftime('%d-%m-%Y')被您的模型字段接受; however, the DateField is stored in your database as the native column of the same type and not as a string with a specific format.但是, DateField作为相同类型的本机列存储在您的数据库中,而不是作为具有特定格式的字符串。 There is a difference between storing the data and representing it in your forms, templates or DRF serializers.存储数据和在表单、模板或 DRF 序列化程序中表示数据之间存在差异。 I really recommend keeping the default format for the database and present the data in the format you want by using theDATE_FORMAT setting to dmY that will take care of presenting your dates in that format as long as the USE_L10N setting is False.我真的建议保留数据库的默认格式,并通过将DATE_FORMAT设置用于dmY以您想要的格式显示数据,只要USE_L10N设置为 False,它将负责以该格式显示您的日期。

if you want to use the same format for display and input, you have to specify input_formats and format(in widget) respectively.如果你想对显示和输入使用相同的格式,你必须分别指定 input_formats 和 format(in widget)。 for example例如

class OrderForm(forms.ModelForm):
deadline = forms.DateTimeField(
    input_formats=['%d/%m/%Y %I:%M %p', ], # input format
    widget=forms.DateTimeInput(format="%d/%m/%Y %I:%M %p"), # initial display format
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM