What time zone does Django use in DateField auto_now_add?

How does Django write a date field when a field is marked with the auto_now_add attribute?

Is this like datetime.now().date() or timezone.now().date() ?

In other words, what time zone is used to get the current date?

+7
python timezone django django-models
source share
2 answers

It looks like it uses datetime.date.today() , which will be the local system date:

db / models / fields / __ init __. py :

 class DateField(Field): ... def pre_save(self, model_instance, add): if self.auto_now or (self.auto_now_add and add): value = datetime.date.today() setattr(model_instance, self.attname, value) return value 

If you need a different behavior, you can remove auto_now_add=True , and then connect the pre_save model to the recipient who set your field to the date you selected.

Alternatively, you can override the save method and set the field there.

+4
source share

Django by default uses the time zone specified in your .py settings with the TIME_ZONE attribute.

The default value is:

 TIME_ZONE = 'UTC' 

More information about this can be found here:

https://docs.djangoproject.com/en/1.7/topics/i18n/timezones/

0
source share

All Articles