diff options
-rw-r--r-- | django/db/backends/oracle/base.py | 6 | ||||
-rw-r--r-- | django/db/models/fields/__init__.py | 1 | ||||
-rw-r--r-- | tests/modeltests/datatypes/__init__.py | 0 | ||||
-rw-r--r-- | tests/modeltests/datatypes/models.py | 60 |
4 files changed, 66 insertions, 1 deletions
diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index bc6f47ed99..082a5ac37c 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -454,7 +454,8 @@ def get_query_set_class(DefaultQuerySet): return select, " ".join(sql), params def resolve_columns(self, row, fields=()): - from django.db.models.fields import DateField, DateTimeField, TimeField + from django.db.models.fields import DateField, DateTimeField, \ + TimeField, BooleanField, NullBooleanField values = [] for value, field in map(None, row, fields): if isinstance(value, Database.LOB): @@ -464,6 +465,9 @@ def get_query_set_class(DefaultQuerySet): # where we undo that treachery. if value == ' ': value = '' + # Convert 1 or 0 to True or False + elif value in (1, 0) and isinstance(field, (BooleanField, NullBooleanField)): + value = bool(value) # cx_Oracle always returns datetime.datetime objects for # DATE and TIMESTAMP columns, but Django wants to see a # python datetime.date, .time, or .datetime. We use the type diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 32624fa7a4..b65b67db5a 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -537,6 +537,7 @@ class DateTimeField(DateField): # neither database supports microseconds. if settings.DATABASE_ENGINE in ('mysql', 'oracle') and hasattr(value, 'microsecond'): value = value.replace(microsecond=0) + value = str(value) return Field.get_db_prep_save(self, value) def get_db_prep_lookup(self, lookup_type, value): diff --git a/tests/modeltests/datatypes/__init__.py b/tests/modeltests/datatypes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/datatypes/__init__.py diff --git a/tests/modeltests/datatypes/models.py b/tests/modeltests/datatypes/models.py new file mode 100644 index 0000000000..979817e721 --- /dev/null +++ b/tests/modeltests/datatypes/models.py @@ -0,0 +1,60 @@ +""" +1. Simple data types testing. + +This is a basic model to test saving and loading boolean and date-related +types, which in the past were problematic for some database backends. +""" + +from django.db import models + +class Donut(models.Model): + name = models.CharField(maxlength=100) + is_frosted = models.BooleanField(default=False) + has_sprinkles = models.NullBooleanField() + baked_date = models.DateField(null=True) + baked_time = models.TimeField(null=True) + consumed_at = models.DateTimeField(null=True) + + class Meta: + ordering = ('consumed_at',) + + def __str__(self): + return self.name + +__test__ = {'API_TESTS': """ +# No donuts are in the system yet. +>>> Donut.objects.all() +[] + +>>> d = Donut(name='Apple Fritter') + +# Ensure we're getting True and False, not 0 and 1 +>>> d.is_frosted +False +>>> d.has_sprinkles +>>> d.has_sprinkles = True +>>> d.has_sprinkles +True +>>> d.save() +>>> d2 = Donut.objects.all()[0] +>>> d2 +<Donut: Apple Fritter> +>>> d2.is_frosted +False +>>> d2.has_sprinkles +True + +>>> import datetime +>>> d2.baked_date = datetime.date(year=1938, month=6, day=4) +>>> d2.baked_time = datetime.time(hour=5, minute=30) +>>> d2.consumed_at = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59) +>>> d2.save() + +>>> d3 = Donut.objects.all()[0] +>>> d3.baked_date +datetime.date(1938, 6, 4) +>>> d3.baked_time +datetime.time(5, 30) +>>> d3.consumed_at +datetime.datetime(2007, 4, 20, 16, 19, 59) +"""} |