Fixed #24841 -- Made BaseRangeField.prepare_value() call base_field's prepare_value()

This commit is contained in:
Villiers Strauss 2015-05-22 15:54:31 +02:00 committed by Tim Graham
parent a6b77abff7
commit 614bec41b5
3 changed files with 20 additions and 4 deletions

View File

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

View File

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

View File

@ -5,7 +5,7 @@ import unittest
from django import forms from django import forms
from django.core import exceptions, serializers from django.core import exceptions, serializers
from django.db import connection from django.db import connection
from django.test import TestCase from django.test import TestCase, override_settings
from django.utils import timezone from django.utils import timezone
from . import PostgresSQLTestCase from . import PostgresSQLTestCase
@ -474,6 +474,14 @@ class TestFormField(PostgresSQLTestCase):
value = field.clean(['2013-04-09 11:45', '']) value = field.clean(['2013-04-09 11:45', ''])
self.assertEqual(value, DateTimeTZRange(datetime.datetime(2013, 4, 9, 11, 45), None)) 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): def test_model_field_formfield_integer(self):
model_field = pg_fields.IntegerRangeField() model_field = pg_fields.IntegerRangeField()
form_field = model_field.formfield() form_field = model_field.formfield()