State sharing and deletion in model inheritance

135 views Asked by At

I have a Django 1.6 model structure where instances share a common model instance as a subclass.

inheritance

In database terms, there is a row for A a row for B and a row for C using the parent_link for ids.

When calling .delete() on C' the row for A and C is deleted but there is an orphan record for B. Instantiating A directly and calling delete will cascade the delete to B and C instances. How can I, in the simplest way, ensure deleting an instance of B or C will delete both the parent and sibling state?

Deleting an instance of A causes linked instances of B and C to be deleted. Deletion of B causes B and A deletion. Deletion of C causes C and A deletion.

How can I have it such that deletion of any A, B or C instance deletes all other instances regardless of the instance delete() is being called on?

As a side note, all instances appear to be deleted, so when calling .delete() at C' it appears that the base state and B' are deleted but infact the row for B' will persist and not appear in queries until another base instance is created where the PK aligns. In that case, B' re-appears from the dead.

Here's the classes:

class A(models.Model):
    # Fields here

class B(A):
    a_ptr = models.OneToOneField(A, parent_link=True)
    # More fields

class C(A):
    a_ptr = models.OneToOneField(A, parent_link=True)
    # More fields

# Code
shared_state = A()
shared_state.save()
sharer_b = B(a_ptr=shared_state.id)
charer_c = C(a_ptr=shared_state.id)
sharer_b.save()
sharer_c.save()

# Objective, this should delete all rows for the above?
sharer_c.delete()
1

There are 1 answers

1
bruno desthuilliers On

You can either override your models delete() method or use the post_delete signal. NB : links are to the current release doc, but all this worked the same in 1.6.