diff --git a/tests/models.py b/tests/models.py index a60a2ff..1657299 100644 --- a/tests/models.py +++ b/tests/models.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.db import models from timezone_field import TimeZoneField diff --git a/tests/tests.py b/tests/tests.py index c3ac6fa..f41cd30 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -289,6 +289,7 @@ class TimeZoneFieldDeconstructTestCase(TestCase): test_fields = ( TimeZoneField(), + TimeZoneField(default='UTC'), TimeZoneField(max_length=42), TimeZoneField(choices=[ (pytz.timezone('US/Pacific'), 'US/Pacific'), @@ -316,6 +317,15 @@ def test_full_serialization(self): # ensuring the following call doesn't throw an error MigrationWriter.serialize(field) + def test_from_db_value(self): + """ + Verify that the field can handle data coming back as bytes from the + db. + """ + field = TimeZoneField() + value = field.from_db_value(b'UTC', None, None, None) + self.assertEqual(pytz.UTC, value) + def test_default_kwargs_not_frozen(self): """ Ensure the deconstructed representation of the field does not contain diff --git a/tests/urls.py b/tests/urls.py new file mode 100644 index 0000000..637600f --- /dev/null +++ b/tests/urls.py @@ -0,0 +1 @@ +urlpatterns = [] diff --git a/timezone_field/fields.py b/timezone_field/fields.py index adbeb50..28388ab 100644 --- a/timezone_field/fields.py +++ b/timezone_field/fields.py @@ -3,6 +3,7 @@ from django.core.exceptions import ValidationError from django.db import models from django.utils import six +from django.utils.encoding import force_text from timezone_field.utils import is_pytz_instance @@ -114,4 +115,8 @@ def _get_python_and_db_repr(self, value): return (pytz.timezone(value), value) except pytz.UnknownTimeZoneError: pass + try: + return (pytz.timezone(force_text(value)), force_text(value)) + except pytz.UnknownTimeZoneError: + pass raise ValidationError("Invalid timezone '%s'" % value) diff --git a/tox.ini b/tox.ini index cb795c7..ec067b3 100644 --- a/tox.ini +++ b/tox.ini @@ -34,4 +34,4 @@ commands = coverage erase [testenv:coverage-report] commands = coverage report - coverage html + coverage html \ No newline at end of file