summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-05-19 13:10:35 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-05-19 13:10:35 +0000
commit593bcc155a7a23654f226fe705a66c1b30e2a6d5 (patch)
tree45962cb06479c2a6206eec946d9d20c95ff5c75d
parentc6f7f2f09227b61b392e56682c51baec24d07108 (diff)
[1.0.X] Fixed #9308 -- Corrected the updated of nullable foreign key fields when deleting objects. Thanks to Bob Thomas for the fix, and markshep for the improvements on the test case.
Merge of r10822 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10823 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/query.py2
-rw-r--r--tests/modeltests/delete/models.py16
2 files changed, 15 insertions, 3 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 7767f5698a..12eebe6555 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -867,7 +867,7 @@ def delete_objects(seen_objs):
update_query = sql.UpdateQuery(cls, connection)
for field, model in cls._meta.get_fields_with_model():
if (field.rel and field.null and field.rel.to in seen_objs and
- filter(lambda f: f.column == field.column,
+ filter(lambda f: f.column == field.rel.get_related_field().column,
field.rel.to._meta.fields)):
if model:
sql.UpdateQuery(model, connection).clear_related(field,
diff --git a/tests/modeltests/delete/models.py b/tests/modeltests/delete/models.py
index 49aab1fb1b..58b296d8d2 100644
--- a/tests/modeltests/delete/models.py
+++ b/tests/modeltests/delete/models.py
@@ -30,7 +30,7 @@ class D(DefaultRepr, models.Model):
# D -> A
# So, we must delete Ds first of all, then Cs then Bs then As.
-# However, if we start at As, we might find Bs first (in which
+# However, if we start at As, we might find Bs first (in which
# case things will be nice), or find Ds first.
# Some mutually dependent models, but nullable
@@ -96,7 +96,7 @@ CyclicDependency: There is a cyclic dependency of items to be processed.
>>> def clear_rel_obj_caches(models):
... for m in models:
-... if hasattr(m._meta, '_related_objects_cache'):
+... if hasattr(m._meta, '_related_objects_cache'):
... del m._meta._related_objects_cache
# Nice order
@@ -168,7 +168,16 @@ True
>>> o.keys()
[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
+# temporarily replace the UpdateQuery class to verify that E.f is actually nulled out first
+>>> import django.db.models.sql
+>>> class LoggingUpdateQuery(django.db.models.sql.UpdateQuery):
+... def clear_related(self, related_field, pk_list):
+... print "CLEARING FIELD",related_field.name
+... return super(LoggingUpdateQuery, self).clear_related(related_field, pk_list)
+>>> original_class = django.db.models.sql.UpdateQuery
+>>> django.db.models.sql.UpdateQuery = LoggingUpdateQuery
>>> e1.delete()
+CLEARING FIELD f
>>> e2 = E()
>>> e2.save()
@@ -185,6 +194,9 @@ True
[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
>>> f2.delete()
+CLEARING FIELD f
+# Put this back to normal
+>>> django.db.models.sql.UpdateQuery = original_class
"""
}