Skip to content

Commit a845a34

Browse files
author
Ryan P Kilby
committed
Add support for microsecond formats for Django<1.6
Relevant django ticket: https://code.djangoproject.com/ticket/19917
1 parent 78c4895 commit a845a34

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

rest_framework_filters/fields.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from django import forms
22

3+
4+
# https://code.djangoproject.com/ticket/19917
5+
class Django14TimeField(forms.TimeField):
6+
input_formats = ['%H:%M:%S', '%H:%M:%S.%f', '%H:%M']
7+
8+
39
class ArrayDecimalField(forms.DecimalField):
410
def clean(self, value):
511
if value is None:

rest_framework_filters/filters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4+
import django
45
from django.utils import six
56
from django_filters.filters import *
67

@@ -33,6 +34,11 @@ class AllLookupsFilter(Filter):
3334
# Fixed-up versions of some of the default filters
3435
###################################################
3536

37+
class TimeFilter(TimeFilter):
38+
if django.VERSION < (1, 6):
39+
field_class = fields.Django14TimeField
40+
41+
3642
class InSetNumberFilter(NumberFilter):
3743
field_class = fields.ArrayDecimalField
3844

rest_framework_filters/filterset.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424

2525

2626
class FilterSet(django_filters.FilterSet):
27-
# In order to support ISO-8601 -- which is the default output for
28-
# DRF -- we need to set up custom date/time input formats.
2927
filter_overrides = {
28+
29+
# In order to support ISO-8601 -- which is the default output for
30+
# DRF -- we need to use django-filter's IsoDateTimeFilter
3031
models.DateTimeField: {
3132
'filter_class': filters.IsoDateTimeFilter,
3233
},
34+
35+
# Django < 1.6 time input formats did not account for microseconds
36+
# https://code.djangoproject.com/ticket/19917
37+
models.TimeField: {
38+
'filter_class': filters.TimeFilter,
39+
}
3340
}
3441

3542
LOOKUP_TYPES = django_filters.filters.LOOKUP_TYPES

0 commit comments

Comments
 (0)