python - django model use database server time -
i have 2 separate django server , mysql server @ difference location
using django model, when try create/update object (as code below). result both 3 column same django server local time value. how db server local time when that.
my model:
class test(models.model): id = models.autofield(primary_key = true, null = false) create = models.datetimefield(auto_now_add=true) update = models.datetimefield(auto_now=true) test = models.datetimefield(null=true)
my code:
y = test() y.test = datetime.now() y.save()
result
id create update test ------ ------------------- ------------------- ------------------- 6 2013-10-07 06:57:04 2013-10-07 06:57:04 2013-10-07 06:57:04
i think can't want without setting database timezone. let's see means each of dates:
create = models.datetimefield(auto_now_add=true)
it sets automatically date time when add it.
update = models.datetimefield(auto_now=true)
it sets automatically date time when start process (either web process or command).
test = models.datetimefield(null=true) ... y.test = datetime.now()
it sets date time of process (where django process run).
to database time, store timezone of database in settings, , use whenever neccesary. can't use automatically generated field this.
another solution grab local time manually (through raw query) on database.
Comments
Post a Comment