简体   繁体   English

如何在 Django 模型中通过“TIME_ZONE”将当前正确的日期和时间分别设置为“DateField()”和“TimeField()”作为默认值?

[英]How to set the current proper date and time to "DateField()" and "TimeField()" respectively as a default value by "TIME_ZONE" in Django Models?

The doc says below in DateField.auto_now_add .该文档在DateField.auto_now_add下面说。 *I use Django 4.2.1 : *我使用Django 4.2.1

Automatically set the field to now when the object is first created.首次创建 object 时自动将字段设置为 now。 ... If you want to be able to modify this field, set the following instead of auto_now_add=True : ...如果您希望能够修改此字段,请设置以下内容而不是auto_now_add=True

  • For DateField : default=date.today - from datetime.date.today()对于DateFielddefault=date.today - from datetime.date.today()
  • For DateTimeField : default=timezone.now - from django.utils.timezone.now()对于DateTimeFielddefault=timezone.now - from django.utils.timezone.now()

So, I set timezone.now and date.today to datetime 's DateTimeField() and date1 's DateField() respectively and I also set current_date which returns timezone.now().date() and current_time which returns timezone.now().time() to date2 's DateField() and time 's TimeField() respectively as shown below:因此,我将timezone.nowdate.today分别设置为datetimeDateTimeField()date1 current_time DateField()并且我还设置了返回timezone.now().date()current_date和返回timezone.now().time()date2DateField()timeTimeField()分别如下所示:

# "models.py"

from django.db import models
from datetime import date
from django.utils import timezone

def current_date():
    return timezone.now().date()

def current_time():
    return timezone.now().time()

class MyModel(models.Model):
    datetime = models.DateTimeField(default=timezone.now) # Here
    date1 = models.DateField(default=date.today) # Here
    date2 = models.DateField(default=current_date) # Here
    time = models.TimeField(default=current_time) # Here

Then, I set 'America/New_York' to TIME_ZONE in settings.py as shown below:然后,我在settings.py中将'America/New_York'设置为TIME_ZONE ,如下所示:

# "settings.py"

LANGUAGE_CODE = "en-us"

TIME_ZONE = 'America/New_York' # Here

USE_I18N = True

USE_L10N = True

USE_TZ = True

But, date1 's DateField() and time 's TimeField() show UTC(+0 hours)'s date and time respectively on Django Admin as shown below:但是, date1DateField()timeTimeField()分别在 Django Admin 上显示 UTC(+0 小时)的日期和时间,如下所示:

在此处输入图像描述

Next, I set 'Asia/Tokyo' to TIME_ZONE in settings.py as shown below:接下来,我在settings.py中将'Asia/Tokyo'设置为TIME_ZONE ,如下所示:

# "settings.py"

LANGUAGE_CODE = "en-us"

TIME_ZONE = 'Asia/Tokyo' # Here

USE_I18N = True

USE_L10N = True

USE_TZ = True

But, date2 's DateField() and time 's TimeField() show UTC(+0 hours)'s date and time on Django Admin as shown below:但是, date2DateField()timeTimeField()在 Django Admin 上显示 UTC(+0 小时)的日期和时间,如下所示:

在此处输入图像描述

So, how can I set the current proper date and time to DateField() and TimeField() respectively as a default value by TIME_ZONE in Django Models?那么,如何在 Django 模型中将当前正确的日期和时间分别设置为DateField()TimeField()作为TIME_ZONE的默认值?

In addition, DateField() and TimeField() with auto_now or auto_now_add cannot save UTC(+0 hours)'s date and time respectively with any TIME_ZONE settings.此外,具有auto_nowauto_now_addDateField()TimeField()无法分别使用任何TIME_ZONE设置保存 UTC(+0 小时)的日期和时间。

The problem is that you set USE_TZ as True .问题是您将USE_TZ设置为True

If USE_TZ is set as True , regardless of what your TIME_ZONE variable is set to, Django will only use the UTC.如果USE_TZ设置为True ,无论您的TIME_ZONE变量设置为什么,Django 都将仅使用 UTC。

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

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