[1.8.x] Fixed #24841 -- Made BaseRangeField.prepare_value() call base_field's prepare_value()

Backport of 614bec41b5 from master
This commit is contained in:
Villiers Strauss 2015-05-22 15:54:31 +02:00 committed by Tim Graham
parent 1f33bf78fd
commit 4311fd2c0a
3 changed files with 20 additions and 4 deletions

View File

@ -22,10 +22,17 @@ class BaseRangeField(forms.MultiValueField):
super(BaseRangeField, self).__init__(**kwargs)
def prepare_value(self, value):
lower_base, upper_base = self.fields
if isinstance(value, self.range_type):
return [value.lower, value.upper]
return [
lower_base.prepare_value(value.lower),
upper_base.prepare_value(value.upper),
]
if value is None:
return [None, None]
return [
lower_base.prepare_value(None),
upper_base.prepare_value(None),
]
return value
def compress(self, values):

View File

@ -9,4 +9,5 @@ Django 1.8.3 fixes several bugs in 1.8.2.
Bugfixes
========
* ...
* Fixed ``BaseRangeField.prepare_value()`` to use each ``base_field``s
``prepare_value()`` method (:ticket:`24841`).

View File

@ -11,7 +11,7 @@ from django.contrib.postgres.validators import (
)
from django.core import exceptions, serializers
from django.db import connection
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils import timezone
from .models import RangesModel
@ -467,6 +467,14 @@ class TestFormField(TestCase):
value = field.clean(['2013-04-09 11:45', ''])
self.assertEqual(value, DateTimeTZRange(datetime.datetime(2013, 4, 9, 11, 45), None))
@override_settings(USE_TZ=True, TIME_ZONE='Africa/Johannesburg')
def test_datetime_prepare_value(self):
field = pg_forms.DateTimeRangeField()
value = field.prepare_value(
DateTimeTZRange(datetime.datetime(2015, 5, 22, 16, 6, 33, tzinfo=timezone.utc), None)
)
self.assertEqual(value, [datetime.datetime(2015, 5, 22, 18, 6, 33), None])
def test_model_field_formfield_integer(self):
model_field = pg_fields.IntegerRangeField()
form_field = model_field.formfield()