Skip to content

Instantly share code, notes, and snippets.

@claudep claudep/21523.diff
Last active Apr 18, 2016

Embed
What would you like to do?
Django issue #21523
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index a4018dd..5acf520 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -1286,14 +1286,14 @@ class DateField(DateTimeCheckMixin, Field):
def to_python(self, value):
if value is None:
return value
- if isinstance(value, datetime.datetime):
+ if hasattr(value, 'tzinfo') and hasattr(value, 'date'):
if settings.USE_TZ and timezone.is_aware(value):
# Convert aware datetimes to the default time zone
# before casting them to dates (#17742).
default_timezone = timezone.get_default_timezone()
value = timezone.make_naive(value, default_timezone)
return value.date()
- if isinstance(value, datetime.date):
+ if hasattr(value, 'year') and hasattr(value, 'month') and hasattr(value, 'day'):
return value
try:
@@ -1383,13 +1383,13 @@ class DateTimeField(DateField):
if not timezone.is_naive(now):
now = timezone.make_naive(now, timezone.utc)
value = self.default
- if isinstance(value, datetime.datetime):
+ if hasattr(value, 'tzinfo') and hasattr(value, 'date'):
second_offset = datetime.timedelta(seconds=10)
lower = now - second_offset
upper = now + second_offset
if timezone.is_aware(value):
value = timezone.make_naive(value, timezone.utc)
- elif isinstance(value, datetime.date):
+ elif hasattr(value, 'year') and hasattr(value, 'month') and hasattr(value, 'day'):
second_offset = datetime.timedelta(seconds=10)
lower = now - second_offset
lower = datetime.datetime(lower.year, lower.month, lower.day)
@@ -1420,9 +1420,9 @@ class DateTimeField(DateField):
def to_python(self, value):
if value is None:
return value
- if isinstance(value, datetime.datetime):
+ if hasattr(value, 'tzinfo') and hasattr(value, 'date'):
return value
- if isinstance(value, datetime.date):
+ if hasattr(value, 'year') and hasattr(value, 'month') and hasattr(value, 'day'):
value = datetime.datetime(value.year, value.month, value.day)
if settings.USE_TZ:
# For backwards compatibility, interpret naive datetimes in
@@ -2240,13 +2240,13 @@ class TimeField(DateTimeCheckMixin, Field):
if not timezone.is_naive(now):
now = timezone.make_naive(now, timezone.utc)
value = self.default
- if isinstance(value, datetime.datetime):
+ if hasattr(value, 'tzinfo') and hasattr(value, 'date'):
second_offset = datetime.timedelta(seconds=10)
lower = now - second_offset
upper = now + second_offset
if timezone.is_aware(value):
value = timezone.make_naive(value, timezone.utc)
- elif isinstance(value, datetime.time):
+ elif hasattr(value, 'minute') and hasattr(value, 'second'):
second_offset = datetime.timedelta(seconds=10)
lower = now - second_offset
upper = now + second_offset
@@ -2288,9 +2288,9 @@ class TimeField(DateTimeCheckMixin, Field):
def to_python(self, value):
if value is None:
return None
- if isinstance(value, datetime.time):
+ if hasattr(value, 'hour') and not hasattr(value, 'day'):
return value
- if isinstance(value, datetime.datetime):
+ if hasattr(value, 'time') and callable(value.time):
# Not usually a good idea to pass in a datetime here (it loses
# information), but this can be a side-effect of interacting with a
# database backend (e.g. Oracle), so we'll be accommodating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.