Skip to content

Can't delete models with inheritance #5

@daggaz

Description

@daggaz

The logic around deleting models is incorrect if model inheritance is used.

For example:

class A(ImmutableModel):
     pass

class B(A):
    pass

class C(B):
    pass

>>> c = C.objects.create()
>>> c.delete()
ValueError: B.a_ptr_id is immutable and cannot be changed

The delete of the C instance is fine, because the overridden c.delete() set the special flag _deleting_immutable_model = True.

When django gets to setting the B instances parent pointer however, this flag is not set (because it's a different object.

Solution
Walk the parent classes of the model being deleted and set _deleting_immutable_model = True on all of them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions