I have a django model with a datetime field like this:
date_creation = models.DateField(auto_now_add=True)
Locally, on my machine, this control is created with the local date and time.
When the application is deployed the field is not created with local time.
I tried to do this:
date_creation = models.DateField(default=datetime.now())
It does not work. How to solve this problem ? I am in central Africa
CodePudding user response:
You should pass a callable, so you can pass the now
function, or better the now
function of Django's timezone
utility:
from datetime import datetime
class MyModel(models.Model):
date_creation = models.DateField(default=datetime.now)
or with timezone.now(…)
[Django-doc]:
from django.utils.timezone import now
class MyModel(models.Model):
date_creation = models.DateField(default=now)
Probably it is however better to specify the timezone in the settings with:
# settings.py
USE_TZ = True
TIME_ZONE = 'Africa/Bangui'