GrantU
GrantU

Reputation: 6555

Cascade delete not working as it should

If I was to delete a Contact object the related Unsubscribe object should cascade delete. It appears this is not the case, why? Is there anything wrong with the Contact model seen below? Thank you.

Unsubscribe

class Unsubscribe(models.Model):
    """

    Notes:
    See: http://www.screamingatmyscreen.com/2012/6/django-and-generic-relations/
    """
    content_type = models.ForeignKey(ContentType, help_text="Represents the name of the model")
    object_id = models.PositiveIntegerField(help_text="stores the object id")
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    reason = models.CharField(max_length=60)

    request_made = models.DateTimeField(auto_now_add=True,
                                   help_text="Shows when object was created.")

Contact:

class Contact(models.Model):
    first_name = models.CharField(max_length=60, blank=True)
    last_name = models.CharField(max_length=60, blank=True)
    created = models.DateTimeField(auto_now_add=True, help_text="Shows when object was created.")

    objects = ContactManager()

    #FK
    group = models.ForeignKey(Group, related_name='contacts', blank=True, null=True)
    contact_owner = models.ForeignKey(User)

Upvotes: 1

Views: 569

Answers (1)

karthikr
karthikr

Reputation: 99680

There is no direct ForeignKey relationship from Contact to Unsubscribe.

In the Unsubscribe model, there is a reference to Contact object by a generic relationship (This translates to just a database table entry that stores object_id and the content_type_id - not the actual foreign key reference ), hence the cascade delete does not work. One way you can delete the corresponding Unsubscribe object is, in a pre_delete signal on the Contact object

Upvotes: 2

Related Questions