silentsokolov / django-admin-rangefilter

A Django app that lets you filter data by date range and numeric range in the admin UI
MIT License
713 stars 106 forks source link

'tzfile' object has no attribute '_utcoffset' #50

Closed ashishnitinpatil closed 4 years ago

ashishnitinpatil commented 4 years ago

I'm trying to use the DateTimeRangeFilter with no fancy customisations. If I instead use the DateRangeFilter, it works as expected. I was digging around and found that it might be a compatibility issue in pytz itself.

The trimmed traceback is as follows -

   File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/views/main.py", line 410, in get_queryset
     new_qs = filter_spec.queryset(request, qs)
   File "/usr/local/lib/python3.8/site-packages/rangefilter/filter.py", line 84, in queryset
     **self._make_query_filter(request, validated_data)
   File "/usr/local/lib/python3.8/site-packages/rangefilter/filter.py", line 215, in _make_query_filter
     query_params['{0}__gte'.format(self.field_path)] = self.make_dt_aware(
   File "/usr/local/lib/python3.8/site-packages/rangefilter/filter.py", line 60, in make_dt_aware
     value = default_tz.normalize(value)
   File "/usr/local/lib/python3.8/site-packages/pytz/tzinfo.py", line 252, in normalize
     offset = dt.tzinfo._utcoffset
 AttributeError: 'tzfile' object has no attribute '_utcoffset'

Relevant dependency versions -

ashishnitinpatil commented 4 years ago

Alright, seems like the issue almost does not belong here, but I'll leave it for those using the lib that may randomly get hit by it. So, I have a middleware that activates the user's timezone at the beginning of every request, and it was made to use dateutil.tz.gettz instead of pytz.timezone function to fetch the timezone (for reasons mentioned in https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html). I have reverted to using pytz in my middleware for now & all seems good.