Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7853 -- Fixed another case of deleting inherited models with f…

…oreign key

references. Thanks to Russell for the test case that demonstrated the problem.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8128 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 19c7db005823efc2da73ca3bcb37ee15451589c8 1 parent 55ba38f
Malcolm Tredinnick authored July 28, 2008
8  django/db/models/query.py
@@ -835,11 +835,15 @@ def delete_objects(seen_objs):
835 835
         del_query.delete_batch_related(pk_list)
836 836
 
837 837
         update_query = sql.UpdateQuery(cls, connection)
838  
-        for field in cls._meta.fields:
  838
+        for field, model in cls._meta.get_fields_with_model():
839 839
             if (field.rel and field.null and field.rel.to in seen_objs and
840 840
                     filter(lambda f: f.column == field.column,
841 841
                     field.rel.to._meta.fields)):
842  
-                update_query.clear_related(field, pk_list)
  842
+                if model:
  843
+                    sql.UpdateQuery(model, connection).clear_related(field,
  844
+                            pk_list)
  845
+                else:
  846
+                    update_query.clear_related(field, pk_list)
843 847
 
844 848
     # Now delete the actual data.
845 849
     for cls in ordered_classes:
12  tests/regressiontests/model_inheritance_regress/models.py
@@ -52,7 +52,12 @@ class Parent(models.Model):
52 52
 class Child(Parent):
53 53
     name = models.CharField(max_length=10)
54 54
 
  55
+class SelfRefParent(models.Model):
  56
+    parent_data = models.IntegerField()
  57
+    self_data = models.ForeignKey('self', null=True)
55 58
 
  59
+class SelfRefChild(SelfRefParent):
  60
+    child_data = models.IntegerField()
56 61
 
57 62
 __test__ = {'API_TESTS':"""
58 63
 # Regression for #7350, #7202
@@ -182,4 +187,11 @@ class Child(Parent):
182 187
 >>> Supplier.objects.filter(restaurant=Restaurant(name='xx', address='yy'))
183 188
 []
184 189
 
  190
+# Regression test for #7853
  191
+# If the parent class has a self-referential link, make sure that any updates
  192
+# to that link via the child update the right table.
  193
+
  194
+>>> obj = SelfRefChild.objects.create(child_data=37, parent_data=42)
  195
+>>> obj.delete()
  196
+
185 197
 """}

0 notes on commit 19c7db0

Please sign in to comment.
Something went wrong with that request. Please try again.