From 8a99d718f7db2d018a37f78a8e5639ff2d1d2aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Sat, 23 Feb 2013 00:00:41 +0200 Subject: [PATCH] [1.5.x] Fixed empty strings + to_field regression on Oracle Querying the reverse side of nullable to_field relation, where both sides can contain null values resulted in incorrect results. The reason was not detecting '' as NULL. Refs #17541, backpatch of 09fcb70c804b76fccc8fc0ac545873e5ab30c00a. --- django/db/models/fields/related.py | 5 +++-- tests/regressiontests/many_to_one_regress/tests.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 387d447ff6..95c865df22 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1,6 +1,6 @@ from operator import attrgetter -from django.db import connection, router +from django.db import connection, connections, router from django.db.backends import util from django.db.models import signals, get_model from django.db.models.fields import (AutoField, Field, IntegerField, @@ -497,7 +497,8 @@ class ForeignRelatedObjectsDescriptor(object): except (AttributeError, KeyError): db = self._db or router.db_for_read(self.model, instance=self.instance) qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters) - if getattr(self.instance, attname) is None: + val = getattr(self.instance, attname) + if val is None or val == '' and connections[db].features.interprets_empty_strings_as_nulls: # We don't want to use qs.none() here, see #19652 return qs.filter(pk__in=[]) qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}} diff --git a/tests/regressiontests/many_to_one_regress/tests.py b/tests/regressiontests/many_to_one_regress/tests.py index e140577a49..a9969a7e90 100644 --- a/tests/regressiontests/many_to_one_regress/tests.py +++ b/tests/regressiontests/many_to_one_regress/tests.py @@ -126,7 +126,7 @@ class ManyToOneRegressionTests(TestCase): # Now the model is saved, so we will need to execute an query. with self.assertNumQueries(1): self.assertEqual(th.child_set.count(), 0) - + def test_related_null_to_field(self): c1 = Car.objects.create() c2 = Car.objects.create()