Django models time zone problem saved to MySQL by Datetimefield

Source: Internet
Author: User
Tags local time

Recently, we started using Django to develop some systems, set up some database table structures in models.py, and assign initial values to date-time fields, but in the process of using it, there was a little problem. The problem is, I used the server to use the urban area is "Asia/shanghai" (+08:00), and then saved to the database with DateTime.Now , the time is always 8 hours longer than my system time (feel UTC time), but I also view my The time zone in the SQL database is "+08:00" (In mysql "SET GLOBAL time_zone = ' +08:00′;") Statement to set the time zone). What is the reason for this ? It took a lot of time today to figure this out.

One of the classes I defined in models.py is as follows (for a table in the database):

class TestSuite (models. Model):  id = models. Autofield (primary_key=true)  name = models. Charfield (max_length=40, blank=true)  description = models. Charfield (max_length=255, blank=true)  bu = models. Charfield (max_length=40, blank=true)  is_enabled = models. Booleanfield (default=true)  create_time = models. Datetimefield (default=datetime.now)  update_time = models. Datetimefield (Default=datetime.now)


Django also has some warning printed out:/users/jay/workspace/te/env/lib/python2.7/site-packages/django/db/models/ Fields/__init__.py:903:runtimewarning:datetimefield Testsuite.update_time received a naive datetime (2014-06-15 14:38:37.873873) while time zone support is active. runtimewarning) here Create_time and update_time time in the database are always in UTC time zone (8 hours slower than Beijing time).

The reason for this warning is that Django is configured to use the TIMEZONE datetime format, and DateTime.Now does not contain timezone information.

If you do not need to specifically process the time zone (timezone-aware) in your program, you can set it to "Use_tz = False" directly in the settings.py file of the Django project. Then, in models.py, the simple setting is "Create_time = models." Datetimefield (Auto_now_add=true) "and" Update_time = models. Datetimefield (Auto_now=true) ".

If you also want to keep use_tz=true, you can set it to Default=datetime.now (). replace (TZINFO=UTC).

I also tried to go through the Django.utils.timezone.now () function to get the current time, and I thought settings.py had set "Time_zone = ' Asia/shanghai '" to get the correct time zone and time But it still gets the UTC time, look at its implementation just a little bit, see below:

def Now (): "" "    Returns A aware or naive datetime.datetime, depending on settings. Use_tz.   """ if settings. Use_tz:   #  Timeit shows, DateTime.Now (TZ=UTC) is 24% slower return Datetime.utcnow (). Replace ( TZINFO=UTC) Else:   return DateTime.Now ()


Or try to use the system local time and time zone through the timezone, for example: Default=timezone.localtime (Timezone.now ()), the results found, is not possible, save to my The datetime in SQL is also the UTC time zone. We have to look at the corresponding implementation code of Django, whether the Datetimefield with the MySQL database, the time to convert to UTC before updating to the database. In addition, if use_tz=true, then setting auto_now_add=true (or auto_now=true) can achieve my purpose? The answer is no, it turns out that Django's handling of the auto_now_add=true is understood, and it is assigned a value of Django.utils.timezone.now ().

Resources:

https://docs.djangoproject.com/en/1.4/ref/settings/#time-zone

Https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#usage

Django models time zone problem saved to MySQL by Datetimefield

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.