在我存储的其中一个模型中 time_stamp = models.DateTimeField(default=timezone.now)
但是当我保存模型时,它说You are 5.5 hours ahead of server time.
例如我的机器中的本地时间是13:02但是在保存存储在 db 中的内容之后是7:16
我在这里得到了一个相关的但它没有一个令人满意的答案......
模型.py
class Comment(models.Model):
time_stamp = models.DateTimeField(default=timezone.now)
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.time_stamp = timezone.now()
return super(Comment, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud) 如何根据本地时间和时区格式化Admin中的DateTimeField?
我的settings.py:
TIME_ZONE = 'Europe/Bratislava'
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Run Code Online (Sandbox Code Playgroud)
pytz包已安装.
模型:
class Material(models.Model):
category = models.ForeignKey(Category, null=True, blank=True)
code = models.CharField(max_length=10)
description = models.CharField(max_length=30, blank=True, null=True)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
还在设置中尝试了一些日期格式化,这没有改变datetime对象在管理列表显示中转换为字符串的方式:
DATETIME_FORMAT = 'd N Y'
DATE_FORMAT = 'd N Y'
Run Code Online (Sandbox Code Playgroud)
在数据库中,datetime正确存储,"2012-11-20 08:57:15.901341 + 01".但是在admin中显示时,它始终是UTC.
我可以在ModelAdmin中准备处理格式的方法,但这并不是真的干,因为我想我的管理类看起来像:
from django.utils.timezone import localtime
class MaterialAdmin(admin.ModelAdmin):
list_display = ('code', 'modified_local', 'created')
def modified_local(self, row):
return localtime(row.modified)
modified_local.admin_order_field = 'modified'
modified_local.short_description = 'Modified'
Run Code Online (Sandbox Code Playgroud) 我正在为我的Django 1.4.3项目创建一个使用South 0.7.6的模式迁移,并启用了时区支持.
模式迁移包括在一个表上添加DateTimeField(with auto_now=True).
在创建迁移时,South提示我:
The field 'MyTable.my_field' does not have a default specified, yet is NOT NULL.
Since you are adding this field, you MUST specify a default
value to use for existing rows. Would you like to:
1. Quit now, and add a default to the field in models.py
2. Specify a one-off value to use for existing columns now
Run Code Online (Sandbox Code Playgroud)
如果我不关心现有行的这个值,那么这里给出的正确的一次性值是多少(我只希望迁移成功而没有警告)?
到目前为止,我用过datetime.datetime.utcnow().但是,在应用迁移时,我得到以下内容:
C:\Python27\lib\site-packages\django\db\models\fields\__init__.py:808:
RuntimeWarning: DateTimeField received a naive datetime (2013-01-16 00:00:00)
while …Run Code Online (Sandbox Code Playgroud) 我想每天午夜运行这个功能来检查expiry_date_notification.我能做什么?我是django和python的新手.
def check_expiry_date(request):
products = Product.objects.all()
for product in products:
product_id = product.id
expiry_date = product.expiry_date
notification_days = product.notification_days
check_date = int((expiry_date - datetime.datetime.today()).days)
if notification_days <= check_date:
notification = Notification(product_id=product_id)
notification.save()
Run Code Online (Sandbox Code Playgroud) 时区让我发疯。每次我认为我已经弄明白了,有人改变了时钟,我得到了十几个错误。我想我终于到了存储正确值的地步。我的时代是timestamp with time zone,我不会在它们被保存之前剥离时区。
TIME_ZONE = 'Europe/London'
USE_I18N = USE_L10N = USE_TZ = True
Run Code Online (Sandbox Code Playgroud)
这是 Postgres 到 dbshell 的特定值:
=> select start from bookings_booking where id = 280825;
2019-04-09 11:50:00+01
Run Code Online (Sandbox Code Playgroud)
但是这里通过 shell_plus 得到了相同的记录
Booking.objects.get(pk=280825).start
datetime.datetime(2019, 4, 9, 10, 50, tzinfo=<UTC>)
Run Code Online (Sandbox Code Playgroud)
这些时间在模板/管理/等中工作正常,但是当我生成 PDF 和电子表格报告时,这一切都出错了,我突然不得不手动重新定位时间。我不明白为什么我必须这样做。数据是本地化的。查询进入数据库和我获取数据之间发生了什么?
我经常碰到这些问题,我在这里对自己完全没有信心——这对于高级开发人员来说非常令人不安——所以我把自己放在你的脚下。那我应该怎么办?
Django的时区感知输出显然仅适用于渲染模板.有没有办法让返回CSV或JSON的响应的当前活动时区进行相同的自动转换?
我有一个按钮应该在模型对象的 start_date 之前 1 天出现。在我看来,我有一个函数可以告诉我对象的 start_date 或 end_date 是否是现在。
def has_engagement(self):
from rental.models import Inquiry
inquiries = Inquiry.objects.filter(inquiryequipment__equipment=self).filter(start_date__lte= timezone.now()).\
filter(end_date__gte=timezone.now()).filter(status="CO")
if not inquiries:
return False
# no current confirmed engagement
else:
return True
# there is a current confirmed engagement
Run Code Online (Sandbox Code Playgroud)
相反,我需要检查今天是否是开始日期或结束日期的前一天。有任何想法吗?谢谢
我知道tzinfo = "America/Los_Angeles"我想要保存到模型的日期时间实例(在哪里).
在保存之前我应该以某种方式将其转换为UTC吗?或者我可以保存原样,因为它知道自己的时区吗?我是否需要稍后将其转换为用户的时区activate(),或者Django会为我做这个,因为实例知道吗?
我最好奇的是这些约定是什么.提前致谢.
我刚刚在 Django Admin 中遇到错误。这是本地重现的日志:
NonExistentTimeError at /admin/{blah}/
2015-10-18 00:00:00
Request Method: GET Request
URL: http://127.0.0.1:8000/admin/{blah}/?q=someuser%40hotmail.com
Django Version: 1.7.10
Exception Type: NonExistentTimeError
Exception Value: 2015-10-18 00:00:00 Exception
Location: C:\Python27\lib\site-packages\pytz\tzinfo.py in localize,
line 327 Python Executable: C:\Python27\python.exe Python
Version: 2.7.10
Run Code Online (Sandbox Code Playgroud)
我发现它确实2015-10-18 00:00:00不存在于我的时区(“美国/圣保罗”)中,因为那是夏令时开始的时间。
我正在使用date_hierarchy = 'date_lastupdated',当我在管理员中搜索用户someuser@hotmail.comDjango 时,仅返回一条记录,而该用户的记录date_lastupdated是'2015-10-18 05:10:18.593336-03'.
错误发生在模板~~~~/Python27/lib/site-packages/grappelli/templates/admin/change_list.html第 212 行:
<!-- DATE HIERARCHY -->
{% block date_hierarchy %}
{% if cl.date_hierarchy %}{% date_hierarchy cl %}{% endif %}
{% …Run Code Online (Sandbox Code Playgroud) 我使用django过滤器后端和Django休息来构建一个休息api.在某些端点,用户可以按日期时间过滤请求,以便进行此操作
/api/api_endpoint_x/?time_start=2015-11-20 11:22:15.160983
Run Code Online (Sandbox Code Playgroud)
会形成一个有效的请求.使用Django对USE_TZ=True所有传入日期时间请求的时区支持被视为在最终用户的本地时间.例如,如果该请求来自柏林,则2015-11-20 10:22:15.160983由于柏林时间为GMT + 1,因此将进行过滤.有没有办法在请求中对时区进行硬编码,以便过滤机制能够使用明确指定的时区?
我知道我可以手动检查查询参数并理解它们然后执行过滤,但我正在研究这个功能已经在里面实现的可能性django-filter.
例如,我可以使用类似的东西
/api/api_endpoint_x/?time_start=2015-11-20 11:22:15.160983+00:00
Run Code Online (Sandbox Code Playgroud)
?
django ×10
django-timezone ×10
python ×5
timezone ×3
django-admin ×2
date ×1
datetime ×1
django-south ×1
postgresql ×1
recurrence ×1