Changes between Version 1 and Version 2 of Ticket #29724, comment 9


Ignore:
Timestamp:
Sep 3, 2019, 4:36:49 PM (5 years ago)
Author:
yab

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29724, comment 9

    v1 v2  
    22
    33#29724 (TZ matter)
    4 When year and month lookup are chosen, if you have an event late in last day of the month that is first day of next month in UTC, queryset.dates method gives a date_hierarchy choice for next month 1st.
    5 It seems it can be solved modifiyng admin/contrib/templatetags/admin_list.py this way, using **queryset.filter and lists that may be very slow** :
    6 Needs importing:
     4Problem occures when : [USE_TZ, sqlite, DatetimeField ] AND object.datetime.date != object datetime.date(tzinfo=UTC), for example In America/Los_Angeles 2018-08-31, 11pm.
     5When year and month lookup are selected, queryset.dates method gives a date_hierarchy choice for next month 1st.
     6It may be solved modifying django/contrib/admin/templatetags/admin_list.py using **queryset.filter (instead of queryset.dates) and lists (that may be slow)** :
    77{{{
    88from django.utils.timezone import make_naive
    99from django.conf import settings
    1010}}}
    11 and after "elif year_lookup and month_lookup:":
     11and in def link(filters):
    1212{{{
    1313                elif year_lookup and month_lookup:
    1414                        ### This is a way to avoid using queryset.dates fucntion, which was giving naive datetimes with problems when the monthrange is different between utc and localtime.
    15 
    1615                        month_filter=field_name+'__month'
    1716                        year_filter=field_name+'__year'
    18                         dates_or_datetimes = cl.model.objects.filter(**{year_filter:year_lookup, month_filter:month_lookup}).values_list(field_name, flat=True).distinct()
    19 
     17                        dates_or_datetimes = cl.model.objects.filter(**{year_filter:year_lookup, month_filter:month_lookup}).values_list(field_name, flat=True)
    2018                        days = dates_or_datetimes
    2119                        if isinstance(dates_or_datetimes[0], datetime.datetime) and settings.USE_TZ:
     
    2624                                                day_list.append(make_naive(day).day)
    2725                                                days.append(make_naive(day))
    28                         ###
     26                       
    2927}}}
    3028
    3129###30749 (DST matter)
    32 If the month selected by date_hierarchy is the month of the DST, the objects of the last day of the month does not appear in the changelist.
    33 Changing the way "to_date" is set after ''elif month'' line 165 solves my problem: Instead of just adding 32 days with timedelta, I use make_aware with tzinfo=None. That way the to_date is set to the correct date and time.
     30In Europe/Paris timezone, on the October DST change, the **from_date + timedelta(days=32)).replace(day=1) ** used in django/contrib/admin/views/main.py to get next month's first day **does not select objects of  October 31th**. They are not shown in the change list when October is selected through date_hierarchy. (steps to reproduce : sqlite, DateField, USE_TZ, TIMEZONE='Europe/Paris', Object date 2019-10-31 or 2018-10-31...).
     31It seems to be resolved by adding make_aware and tzinfo=None to to_date :
     32django/contrib/admin/views/main.py:
    3433{{{
    3534                                elif month:
    36                                         ### The tzinfo changes between first and last day of the month when DST applies.  That's why we need to make_aware to_date separately from from_date
    3735                                        to_date = make_aware((from_date + timedelta(days=32)).replace(day=1,tzinfo=None))
    3836}}}
Back to Top