I have an Event model that includes a “start time” field, which is stored in UTC and mapped to the DB. The model also has a start_local() helper function the returns the event start as a Datetime object in the user’s local time. This happens via the Event instance’s reference to User, then get_profile() which has the timezone information.
This works, but the issue is that when I display long lists of events, there are 2 DB queries per row due to the user and profile lookups. This is silly because the timezone isn’t variable between rows, but the model doesn’t know that. My current setup passes the timezone information to start_local(), and that fixes the speed problem but is not a very pleasant from a design perspective. The Event instance knows how to get the user’s timezone, and having to rely on the calling function for help to avoid a big performance his is annoying.
Does anyone have an improvement to passing timezone information into the helper function?
Use
select_related. I forget when OneToOne select related was introduced, but it works on 1.3.https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.select_related