Code

Opened 5 years ago

Closed 5 years ago

#10262 closed (duplicate)

Allow to clear object before deleting to avoid data loss because delete cascade behaviour

Reported by: msaelices Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: carljm Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Imaging a model like this:

class Master(models.Model):
    name = models.CharField(max_length=100)
        

class Slave(models.Model):
    name = models.CharField(max_length=100)
    master = models.ForeignKey(Master, null=True)

And this object creation:

>>> m1 = Master.objects.create(name="master1")
>>> s1 = Slave.objects.create(name="slave1", master=m1)

>>> m1.delete() # this will also delete s1

Django by default, delete in cascade all related objects... even with null=True.

If you want to avoid this, you can do this:

>>> m1.slave_set.clear()
>>> m1.delete()

This is ok, but it was wonderful if I can change behaviour to not delete cascade. You can think in admin site for example. If you want to delete a Category object you have previously to enter in maybe a hundred objects to set null by hand.

I propose a definition like this:

class Slave(models.Model):
    name = models.CharField(max_length=100)
    master = models.ForeignKey(Master, null=True, delete_cascade=False)

With delete_cascade=False (by default would be True), delete() method will clear this relation previously deletion.

I've attached a patch that implements this idea.

Attachments (3)

delete_cascade_option_r9832.diff (3.1 KB) - added by msaelices 5 years ago.
delete_cascade_option_with_admin_r9834.diff (3.7 KB) - added by msaelices 5 years ago.
Updated admin to not display objects not deleted (because delete_cascade was False)
delete_cascade_option_with_tests_r9834.diff (7.1 KB) - added by msaelices 5 years ago.
With unit tests and with delete_cascade for OneToOneRel

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by msaelices

Changed 5 years ago by msaelices

Updated admin to not display objects not deleted (because delete_cascade was False)

Changed 5 years ago by msaelices

With unit tests and with delete_cascade for OneToOneRel

comment:1 Changed 5 years ago by kmike

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 5 years ago by kmike

  • Needs documentation set

comment:3 Changed 5 years ago by carljm

  • Cc carljm added

comment:4 Changed 5 years ago by ikelly

This looks like a duplicate of #7539.

comment:5 Changed 5 years ago by jacob

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #7539

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.