id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 22536 Exceptions using date-based generic views with MySQL, USE_TZ=True and bad data Chris Adams Chris Adams "I ran into a non-obvious exception while using the date-based generic views. get_previous_day / get_next_day were raising an AttributeError ('NoneType' object has no attribute 'astimezone') in the following code when the view was called with the earliest item in the system: {{{ try: result = getattr(qs[0], date_field) except IndexError: return None # Convert datetimes to dates in the current time zone. if generic_view.uses_datetime_field: if settings.USE_TZ: result = timezone.localtime(result) result = result.date() }}} This error wasn't immediately obvious because the query shouldn't have been able to return a NULL. Looking at the data revealed the problem: a small number of records on my test instance had invalid dates in MySQL (0000-00-00) which were included by the __lt query but subsequently converted into None by the ORM. The nature of the bug suggests that the cleanest patch might simply be an immediate ""if not result: return None"", perhaps with a warning about invalid data." Bug new Generic views 1.5 Normal Accepted 0 0 0 0 0 0