JorgeParada
JorgeParada

Reputation: 603

Django DatetimeField change on update with incorrect value

I got a Model like this:

class Menu(models.Model):    
 version = models.BigIntegerField(blank=False, null=False)
 local = models.ForeignKey(Local, db_column='id_local')
 created = models.DateTimeField(auto_now_add=True, blank=True,db_column='created')
 actived = models.DateTimeField(db_column='actived')
 class Meta:
    managed = False
    db_table = 'menu'

When i created a object of that; all works easy and well. But i got problems updating 'actived'. I use a code like:

menu = Menu.objects.get(version=ver, local=local) 
menu.actived = datetime.now()
menu.save()

The actived value was correct; when i see it in db (MySql, with utf-8 charset) or in template, the value corresponds. But the value of 'menu.created' change to the corresponding of 'menu.created - 1 HOUR'.

I don´t know why; and that´s all the code that i modify.

Upvotes: 2

Views: 2249

Answers (2)

JorgeParada
JorgeParada

Reputation: 603

Finally, i got the solution.

First of all; thanks Kevin Christopher Henry.

In my settings.py i was changed this 3 settings and all works fine.

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = False

Upvotes: 2

wolendranh
wolendranh

Reputation: 4292

It is possible that you have problem with Timezones.Check this documents

And better way is to do this like:

created = models.DateTimeField(default=datetime.datetime.now)

Upvotes: 0

Related Questions