본문 바로가기
Django

RuntimeWarning: DateTimeField received a naive datetime

by Going9 2023. 9. 13.
price_history = PriceHistory(
    type="cpu",
    part_id=existing_cpu.id,
    start_date=datetime.datetime.now(),
    price=price
)
price_history.save()

위 코드는 가격 추적 모델을 업데이트 하는 코드이다.

그런데 위 코드에서 업데이트 하는 시간을 저장할 때 날짜와 시간 정보만을 가지고 있고, 어떤 특정 시간대(time zone)와 관련된 정보가 없다.

그래서 아래와 같이 시간대에 관한 정보를 주어야한다. 그럼 더이상 나이브한 객체가 아니라 timezone-aware한 형식으로 저장된다. 

price_history = PriceHistory(
    type="cpu",
    part_id=existing_cpu.id,
    start_date=timezone.now(),
    price=price
)
price_history.save()