您好,我想要服务old book sale service for university student
我创建了ItemPost模型,当用户发布他们的图书时,ItemPost的截止日期保存在deadline
from django.db import models
from django.conf import settings
from django.utils import timezone
def localtime():
return timezone.localtime(timezone.now())
class ItemPost(models.Model):
title = models.TextField(
)
created_at = models.DateTimeField(
default=localtime
)
is_deleted = models.BooleanField(
default=False,
verbose_name="삭제된 포스트",
)
# 마감날짜를 구하는 함수
def deadline_def(self):
year_of_item_created = self.created_at.year
if self.created_at.month <= 6:
return timezone.datetime(year_of_item_created, 6, 30)
else:
return timezone.datetime(year_of_item_created, 12, 31)
deadline = property(deadline_def)
# 등록된 학기가 끝난 포스트인지 확인
def is_ended_semester_def(self):
now = timezone.now()
if now > self.deadline:
return True
return False
is_ended_semester = property(is_ended_semester_def)
def __str__(self):
return self.title
我想要比较项目的截止日期,timezone.now()
并返回True或False
但如果我命令item.is_ended_semester
TypeError: can't compare offset-naive and offset-aware datetimes
我怎样才能解决这个问题?
item.deadline
> datetime.datetime(2017, 6, 30, 0, 0)
timezone.now()
> datetime.datetime(2017, 7, 14, 8, 50, 57, 91304, tzinfo=<UTC>)
答案 0 :(得分:0)
我使用timezone.utc
# 마감날짜를 구하는 함수
def deadline_def(self):
year_of_item_created = self.created_at.year
if self.created_at.month <= 6:
return timezone.datetime(year_of_item_created, 6, 30, tzinfo=timezone.utc)
else:
return timezone.datetime(year_of_item_created, 12, 31, tzinfo=timezone.utc)