diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index efec3e58f1..c5da7ac25c 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -466,10 +466,11 @@ def get_query_set_class(DefaultQuerySet): for value, field in map(None, row, fields): if isinstance(value, Database.LOB): value = value.read() - # Since Oracle won't distinguish between NULL and an empty - # string (''), we store empty strings as a space. Here is - # where we undo that treachery. - if value == ' ': + # Oracle stores empty strings as null. We need to undo this in + # order to adhere to the Django convention of using the empty + # string instead of null, but only if the field accepts the + # empty string. + if value is None and field.empty_strings_allowed: value = '' # Convert 1 or 0 to True or False elif value in (1, 0) and isinstance(field, (BooleanField, NullBooleanField)): diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index f9aed8b98e..0afd8471fd 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -76,6 +76,10 @@ class Field(object): self.primary_key = primary_key self.maxlength, self.unique = maxlength, unique self.blank, self.null = blank, null + # Oracle treats the empty string ('') as null, so coerce the null + # option whenever '' is a possible value. + if self.empty_strings_allowed and settings.DATABASE_ENGINE == 'oracle': + self.null = True self.core, self.rel, self.default = core, rel, default self.editable = editable self.serialize = serialize @@ -162,10 +166,6 @@ class Field(object): def get_db_prep_save(self, value): "Returns field's value prepared for saving into a database." - # Oracle treats empty strings ('') the same as NULLs. - # To get around this wart, we need to change it to something else... - if settings.DATABASE_ENGINE == 'oracle' and value == '': - value = ' ' return value def get_db_prep_lookup(self, lookup_type, value):