0

In my django project i have a model where i store data (in my read_date field) with a DateTime field for storing date (with timezone):

class Results(models.Model):
    id = models.AutoField(primary_key=True)
    device = models.ForeignKey(Device, null=True, on_delete=models.SET_NULL)
    proj_code = models.CharField(max_length=400)
    res_key = models.SlugField(max_length=80, verbose_name="Message unique key", unique=True)
    read_date = models.DateTimeField(verbose_name="Datetime of vals readings")
    unit = models.ForeignKey(ModbusDevice, null=True, on_delete=models.SET_NULL)

i setted in settings.py file using timezone:

TIME_ZONE = 'UTC+1'

USE_TZ = True

So, the problem is, when i pass a corrected formatted ditetime with UTC+1 timezone as:

2021-11-12 10:39:59.495396+01:00

in my db i get:

2021-11-12 9:39:59.495396+00

Why my stored value is UTC+0 instead using timezone passed?

So many thanks in advance

Manuel Santi
  • 1,106
  • 17
  • 46

1 Answers1

0

All the date-times are stored in the UTC. It's not a bug it's a feature. The database needs one format to process the DateTime data when sorting, selecting, filtering, etc. So when you are making some SQL requests you can pass a needed timezone for auto converting. For example, your first user in the UTC+1 and you can make requests using this parameter, your another user UTC-9, and the results could be different.

fanni
  • 1,149
  • 8
  • 11