Opened 15 years ago

Last modified 2 years ago

#12382 new Bug

Deleting an inline can delete the whole inlined model instance recursively

Reported by: Dirk Eschler Owned by: nobody
Component: contrib.admin Version: 1.1
Severity: Normal Keywords: admin inlines
Cc: eschler@…, bugs@…, Carsten Fuchs Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

A deleted inline can delete the whole inlined model (and all its related ForeignKeys) if the inlined model has a ForeignKey back to model it inlines. As this is best illustrated by an example, here's some code:

models.py:

class Telephone(models.Model):
    TELEPHONE_TYPES = ( ('WORK', 'Work'),
                        ('HOME', 'Home'),
                      )
    telephone_type = models.CharField(max_length=16, choices=TELEPHONE_TYPES)
    number = models.CharField(max_length=100)
    contact = models.ForeignKey('Contact')
    # ...

class Contact(models.Model):
    family_name = models.CharField(max_length=255, null=True, blank=True)
    given_name = models.CharField(max_length=255, null=True, blank=True)
    # ...
    preferred_telephone = models.ForeignKey(Telephone, related_name='preferred_telephone', null=True, blank=True)

admin.py:

from

class TelephoneInline(admin.TabularInline):
    model = Telephone

class TelephoneAdmin(admin.ModelAdmin):
    # ...
    
class ContactAdmin(admin.ModelAdmin):
    # ...
    inlines = [
        TelephoneInline,
    ]

Steps:

  • Create a contact with a telephone number through the admin and save it
  • Choose the new telephone number as preferred_telephone and save the contact
  • Check the delete checkbox for the telephone number
  • Save

Expected:

The telephone number is deleted.

Result:

The contact is deleted recursively! In addition a 404 is raised that the contact doesn't exist, which is true.

If the Contact model had more relations, like a ForeignKey relation to an Address model for instance, all the addresses of the contact would have been deleted, too.

Change History (11)

comment:1 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

The deletion itself is a case of working as designed. #7539 exists to address the control of delete cascading behavior.

This leaves the UI problem - if deleting an inline leads to deleting the base object, you shouldn't get a 404. We track the objects that are going to be deleted; that could be used as a basis for redirection (i.e., if self on list, redirect to the "model" page in the admin)

comment:2 by anonymous, 15 years ago

Cc: eschler@… added

comment:3 by Carl Meyer, 14 years ago

#7539 is now fixed; the deletion here, if undesired, can be resolved by setting the on_delete argument to the ForeignKey appropriately.

The 404 / UI issue here remains.

comment:4 by Matt McClanahan, 14 years ago

Severity: Normal
Type: Bug

comment:5 by Julien Phalip, 14 years ago

UI/UX: set

comment:6 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

UI/UX: set

Revert accidental batch modification.

comment:9 by Tim Graham, 9 years ago

#14411 is a duplicate with long discussion.

comment:10 by ris, 9 years ago

Cc: bugs@… added

comment:11 by Carsten Fuchs, 2 years ago

Cc: Carsten Fuchs added
Note: See TracTickets for help on using tickets.
Back to Top