#10261: Allow to clear object before deleting to avoid data loss because delete
cascade behaviour
------------------------------------------+---------------------------------
 Reporter:  msaelices                     |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  1.0       
 Keywords:                                |       Stage:  Unreviewed
Has_patch:  1                             |  
------------------------------------------+---------------------------------
 Imaging a model like this:
 {{{
 #!python
 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:
 {{{
 #!python
 >>> 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:
 {{{
 #!python
 >>> 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:
 {{{
 #!python
 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.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/10261>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to