Opened 8 years ago

Closed 8 years ago

#26585 closed Bug (needsinfo)

Cascade-deletion through generic foreign keys ignores field names

Reported by: Adam Nickle Owned by: nobody
Component: contrib.contenttypes Version: 1.8
Severity: Normal Keywords: genericforeignkey delete
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tobin Brown)

Create three models:

from django.db import models
from django.contrib.contenttypes import generic


class Location(models.Model):
    address_line1 = models.CharField()

class Account(models.Model):
    acct_number = models.CharField()

class Link(models.Model):
    account = models.ForeignKey(Account)
    linked_object_id = models.PositiveIntegerField()
    linked_object_content_type = models.ForeignKey(ContentType)
    linked_object = generic.GenericForeignKey('linked_object_content_type', 'linked_object_id')

With this setup, deleting a Location object will attempt to cascade-delete any associated Link objects. In doing so, Django ignores the parameters to the GenericForeignKey field constructor and instead expects the content type to be in a field called content_type and the object id in object_id. This prevents cascade deletion through generic foreign keys from working whenever these fields aren't named as expected or whenever there are multiple generic foreign keys in a model.

Change History (4)

comment:1 by Adam Nickle, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

Could you please verify that the problem exists on master and also include a test case that reproduces the issue?

comment:3 by Tobin Brown, 8 years ago

Description: modified (diff)

comment:4 by Simon Charette, 8 years ago

Resolution: needsinfo
Status: newclosed

The report looks a bit suspicious to me.

In order to make Location deletion cascades to Link a GenericRelation has to be defined on the former which is not the case here.

Please re-open this ticket if you can provide more details about your model definitions and an actual traceback.

Note: See TracTickets for help on using tickets.
Back to Top