datetime.datetime.now returning "received a naive datetime while time zone support is active"

23 Views Asked by At

in my project i have multiple apps , and multiple models , in some models i define time = datetime.datetime.now() , however when i do .save() to save the object to the database i get the warning "received a naive datetime while time zone support is active" . I dont want this warning , i know that i can add zonal information (using timezone) to remove this warning however adding it manually everywhere throughout the project would be tedious. i also tried adding USE_TZ = False to the settings file however i have models where i want the zonal information to be enabled. So basically i want my project to support both cases where we pass and dont pass zonal information, i dont want the warning to be displayed when saving an object and i dont want to add timezone manually

eg:

a = Apple.objects.create()
a.start = datetime.datetime.now()
a.save()

RuntimeWarning: DateTimeField Shift.expected_in_time received a naive datetime (2024-03-28 13:00:36) while time zone support is active. I dont want this warning `

a = Apple.objects.create()
aware_datetime = aware_datetime = timezone.make_aware(datetime.datetime.now(), timezone=timezone.get_current_timezone())
a.start = aware_datetime
a.save()

` now no warning is seen , however changing it manually everywhere is time consuming , is there a faster way to do it

0

There are 0 best solutions below