diff --git a/django/contrib/postgres/forms/ranges.py b/django/contrib/postgres/forms/ranges.py index 47a16a1bbd..5a20975eb4 100644 --- a/django/contrib/postgres/forms/ranges.py +++ b/django/contrib/postgres/forms/ranges.py @@ -2,20 +2,38 @@ from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange from django import forms from django.core import exceptions -from django.forms.widgets import MultiWidget +from django.forms.widgets import HiddenInput, MultiWidget from django.utils.translation import gettext_lazy as _ __all__ = [ 'BaseRangeField', 'IntegerRangeField', 'DecimalRangeField', - 'DateTimeRangeField', 'DateRangeField', 'RangeWidget', + 'DateTimeRangeField', 'DateRangeField', 'HiddenRangeWidget', 'RangeWidget', ] +class RangeWidget(MultiWidget): + def __init__(self, base_widget, attrs=None): + widgets = (base_widget, base_widget) + super().__init__(widgets, attrs) + + def decompress(self, value): + if value: + return (value.lower, value.upper) + return (None, None) + + +class HiddenRangeWidget(RangeWidget): + """A widget that splits input into two inputs.""" + def __init__(self, attrs=None): + super().__init__(HiddenInput, attrs) + + class BaseRangeField(forms.MultiValueField): default_error_messages = { 'invalid': _('Enter two valid values.'), 'bound_ordering': _('The start of the range must not exceed the end of the range.'), } + hidden_widget = HiddenRangeWidget def __init__(self, **kwargs): if 'widget' not in kwargs: @@ -82,14 +100,3 @@ class DateRangeField(BaseRangeField): default_error_messages = {'invalid': _('Enter two valid dates.')} base_field = forms.DateField range_type = DateRange - - -class RangeWidget(MultiWidget): - def __init__(self, base_widget, attrs=None): - widgets = (base_widget, base_widget) - super().__init__(widgets, attrs) - - def decompress(self, value): - if value: - return (value.lower, value.upper) - return (None, None) diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py index c18c351031..3702221107 100644 --- a/tests/postgres_tests/test_ranges.py +++ b/tests/postgres_tests/test_ranges.py @@ -5,6 +5,7 @@ from decimal import Decimal from django import forms from django.core import exceptions, serializers from django.db.models import DateField, DateTimeField, F, Func, Value +from django.http import QueryDict from django.test import override_settings from django.utils import timezone @@ -513,8 +514,9 @@ class TestFormField(PostgreSQLSimpleTestCase):