当我尝试通过管理员将我的两个模型中的日期时间字段设置为今天的日期时,我收到错误“无法在时区 America/Los_Angeles 中解释;它可能不明确或可能不存在”。这只发生在这两种模型中。如果我尝试在其他模型中将 datetime 设置为今天的日期(其中大部分未显示),则没有问题。
以下是 model.py 中的相关模型:
from django.db import models
# DateTimeField has no problem with today's date in this model
class Subject(models.Model):
title = models.CharField(max_length=200, unique=True)
date_created = models.DateTimeField('date created')
times_viewed = models.IntegerField()
# Both DateTimeFields give an error in this model
class Discussion(models.Model):
subject = models.ForeignKey(Subject)
title = models.CharField(max_length=200)
version = models.CharField(max_length=200)
created = models.DateTimeField('date created')
updated = models.DateTimeField('date updated')
creator = models.CharField(max_length=200)
# DateTimeField gives an error in this model too
class DiscussionPost(models.Model):
discussion = models.ForeignKey(Discussion)
poster = models.CharField(max_length=200)
text = models.TextField()
posted = models.DateTimeField('date posted')
这是 admin.py 的相关部分:
from django.contrib import admin
from my_app.models import Subject, Discussion, DiscussionPost # and other irrelevant models
class DiscussionPostInline(admin.StackedInline):
model = DiscussionPost
extra = 1
class DiscussionAdmin(admin.ModelAdmin):
fieldsets = [
('Title', {'fields': ['title']}),
('Creator', {'fields': ['creator']}),
('Date Created', {'fields': ['created']}),
('Date Updated', {'fields': ['updated']}),
]
inlines = [DiscussionPostInline]
list_display = ('title', 'creator', 'created', 'updated')
admin.site.register(Discussion, DiscussionAdmin)
DiscussionInline(admin.StackedInline):
model = Discussion
extra = 1
SubjectAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['title']}),
('Times Viewed', {'fields': ['times_viewed'], 'classes': ['collapse']}),
('Date Created', {'fields': ['date_created'], 'classes': ['collapse']}),
]
inlines = [DiscussionInline]
list_display = ('title', 'times_viewed', 'date_created')
list_filter = ['date_created']
search_fields = ['title']
date_hierarchy = 'date_created'
admin.site.register(Subject, SubjectAdmin)
如果我从管理员手动更改为不同的日期,我不会收到错误消息。就在我使用今天的日期时(手动和使用 now() )。有人知道为什么会这样吗?
此管理结构基于此Django Admin nested inline的第二个答案。
更新我在管理员中更改了日期时间,现在它可以工作了。我没有更改模型或管理中的任何内容,所以我很难理解为什么今天早上早些时候它没有工作。