Skip to content

Commit b9ddc12

Browse files
PiDelportmichaeljohnbarr
authored andcommitted
Django 2.0 support (#9)
* Travis config: Add Django 2.0 * Django 2.0: Add on_delete to ForeignKey fields * Travis: Skip Python 3.2 and 3.3 for Django 2.0 too * Accommodate Model.from_db_value() signature change in Django 2.0 This avoids a RemovedInDjango30Warning in Django 2.0.
1 parent 4b601ec commit b9ddc12

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ python:
1414

1515
env:
1616
- DJANGO="https://github.com/django/django/archive/master.tar.gz"
17+
- DJANGO="django>=2.0,<2.1"
1718
- DJANGO="django>=1.10,<1.11"
1819
- DJANGO="django>=1.9,<1.10"
1920
- DJANGO="django>=1.8,<1.9"
@@ -52,6 +53,12 @@ matrix:
5253
# Django 1.11 requires Python 2.7, 3.4, or 3.5.
5354
# The Django 1.11.x series is the last to support Python 2.
5455
# The next major release, Django 2.0, will only support Python 3.5+
56+
- python: "2.7"
57+
env: DJANGO="django>=2.0,<2.1"
58+
- python: "3.2"
59+
env: DJANGO="django>=2.0,<2.1"
60+
- python: "3.3"
61+
env: DJANGO="django>=2.0,<2.1"
5562
- python: "3.2"
5663
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
5764
- python: "3.3"

tests/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ def get_other_model_timezone(obj):
162162
class ModelWithForeignKeyToTimeZone(models.Model):
163163
other_model = models.ForeignKey(
164164
to='tests.TZWithGoodStringDefault',
165-
related_name='fk_to_tz'
165+
related_name='fk_to_tz',
166+
on_delete=models.CASCADE,
166167
)
167168
timestamp = LinkedTZDateTimeField(
168169
default=settings.TEST_DATETIME,
@@ -189,7 +190,8 @@ class ModelWithLocalTZCharField(models.Model):
189190
class TZTimeFramedModel(models.Model):
190191
other_model = models.ForeignKey(
191192
to='tests.TZWithGoodStringDefault',
192-
related_name='fk_to_tz_too'
193+
related_name='fk_to_tz_too',
194+
on_delete=models.CASCADE,
193195
)
194196
start = LinkedTZDateTimeField(
195197
default=settings.TEST_DATETIME,

timezone_utils/fields.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99

1010
# Django
11+
import django
1112
from django.core import checks
1213
from django.core.exceptions import ValidationError
1314
from django.db.models.fields import DateTimeField, CharField
@@ -83,15 +84,26 @@ def get_prep_value(self, value):
8384

8485
return value
8586

86-
def from_db_value(self, value, expression, connection, context): # noqa
87-
"""
88-
Converts a value as returned by the database to a Python object. It is
89-
the reverse of get_prep_value(). - New in Django 1.8
90-
"""
91-
if value:
92-
value = self.to_python(value)
93-
94-
return value
87+
# Django 2.0 updates the signature of from_db_value.
88+
# https://docs.djangoproject.com/en/2.0/releases/2.0/#context-argument-of-field-from-db-value-and-expression-convert-value
89+
if django.VERSION < (2,):
90+
def from_db_value(self, value, expression, connection, context): # noqa
91+
"""
92+
Converts a value as returned by the database to a Python object. It is
93+
the reverse of get_prep_value(). - New in Django 1.8
94+
"""
95+
if value:
96+
value = self.to_python(value)
97+
return value
98+
else:
99+
def from_db_value(self, value, expression, connection):
100+
"""
101+
Converts a value as returned by the database to a Python object. It is
102+
the reverse of get_prep_value(). - New in Django 1.8
103+
"""
104+
if value:
105+
value = self.to_python(value)
106+
return value
95107

96108
def to_python(self, value):
97109
"""Returns a datetime.tzinfo instance for the value."""

0 commit comments

Comments
 (0)