[英]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
:
DateField
: default=date.today
- from datetime.date.today()
DateField
: default=date.today
- from datetime.date.today()
DateTimeField
: default=timezone.now
- from django.utils.timezone.now()
DateTimeField
: default=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.now和date.today分别设置为
datetime
的DateTimeField()和date1
current_time
DateField()并且我还设置了返回timezone.now().date()
的current_date
和返回timezone.now().time()
到date2
的DateField()
和time
的TimeField()分别如下所示:
# "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:但是,
date1
的DateField()
和time
的TimeField()
分别在 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:但是,
date2
的DateField()
和time
的TimeField()
在 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_now或auto_now_add的
DateField()
和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.