擅长:python、mysql、java
<p>您需要分两步进行:</p>
<ol>
<li>首先,让datetime时区有意识-在你的例子中,使用CET(中欧时间)时区。你知道吗</li>
<li>其次,在保存日期时间之前,将其转换为UTC。这是因为<a href="https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#overview" rel="nofollow">Django stores datetimes as UTC</a>:</li>
</ol>
<blockquote>
<p>When support for time zones is enabled, Django stores datetime
information in UTC in the database...</p>
</blockquote>
<p>可以这样做:</p>
<pre><code>import pytz
from django.utils import timezone
# Convert the naive datetime into a CET datetime
local_datetime = timezone.make_aware(naive_datetime, pytz.timezone('CET'))
# Convert the CET datetime to UTC
utc_datetime = local_datetime.astimezone(timezone.utc)
# Now it can be saved in the database on a model field
MyDateModel.objects.create(title='Another date model',
created=utc_datetime)
</code></pre>