diff --git a/django/db/models/query.py b/django/db/models/query.py index 92395a3c4a..7f9fc3e5d9 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -835,11 +835,15 @@ def delete_objects(seen_objs): del_query.delete_batch_related(pk_list) update_query = sql.UpdateQuery(cls, connection) - for field in cls._meta.fields: + 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, field.rel.to._meta.fields)): - update_query.clear_related(field, pk_list) + if model: + sql.UpdateQuery(model, connection).clear_related(field, + pk_list) + else: + update_query.clear_related(field, pk_list) # Now delete the actual data. for cls in ordered_classes: diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py index fb1fd62798..716c7f5ad8 100644 --- a/tests/regressiontests/model_inheritance_regress/models.py +++ b/tests/regressiontests/model_inheritance_regress/models.py @@ -52,7 +52,12 @@ class Parent(models.Model): class Child(Parent): name = models.CharField(max_length=10) +class SelfRefParent(models.Model): + parent_data = models.IntegerField() + self_data = models.ForeignKey('self', null=True) +class SelfRefChild(SelfRefParent): + child_data = models.IntegerField() __test__ = {'API_TESTS':""" # Regression for #7350, #7202 @@ -182,4 +187,11 @@ True >>> Supplier.objects.filter(restaurant=Restaurant(name='xx', address='yy')) [] +# Regression test for #7853 +# If the parent class has a self-referential link, make sure that any updates +# to that link via the child update the right table. + +>>> obj = SelfRefChild.objects.create(child_data=37, parent_data=42) +>>> obj.delete() + """}