Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Relying on date/time 'automagic' is dangerous and these auto_add model parameters are a trap. Always understand the timezone(s) you are dealing with. Python makes this easier by attaching a tzinfo member to its datetime objects. While these objects are 'naive' by default, I encourage you to always attach tzinfo detail. Still Python needs some extra help with either <em>python-dateutil</em> or <em>pytz</em> (what I use). Here's a universal rule though - always store your datetimes in a database as UTC.</p> <p>Why? Your users may be in different locals, mobile phones and laptops travel, servers are misconfigured or mirrored in different timezones. So many headaches. Datetimes should never be naive and if they are (as in a database) and you need the context, also include a timezone field in the table.</p> <p>So in your case. </p> <ol> <li>Don't use the auto_now fields, use a custom save() instead.</li> <li>Store UTC in the database</li> <li>If you need to know the timezone - for say a user event - store the timezone in the database as well.</li> <li>Convert to the necessary/requested timezone</li> </ol> <p>If you are using pytz, the <a href="http://pytz.sourceforge.net/#localized-times-and-date-arithmetic" rel="nofollow noreferrer">localize()</a> method is great. Python's datetime object has the useful replace() and astimezone().</p> <p>One more note, if your database is timezone naive (like MySQL) make sure your datetimes are in UTC and then use replace(tzinfo=None) because the database connector can't handle tz-aware objects.</p> <p>Here is a <a href="https://stackoverflow.com/questions/1737017/django-auto-now-and-auto-now-add">thread</a> with detail on Django's auto_now fields.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload