Opened 16 years ago

Closed 15 years ago

Last modified 14 years ago

#8168 closed (duplicate)

Need another Pre Delete Signal Type (feature)

Reported by: magneto Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords: signals
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

the Pre delete signal is not all its cracked up to be ...

there should be another signal which is 'pre_prepare_for_delete'

This issuse is this

suppose i have 2 apps, all foreignkeyed to a user

####################
#Ap 1
class M1(models.Model):
  user = models.ForeignKey(User)
  very_transactional_stuff = models.CharFeld(max_length = 4)

def on_delete_user(sender, instance, *args, **kwargs):
     ASSIGN_TO_ROOT_ON_DELETE, created = User.object.get_or_create(username = 'UserDeleted')
     for m in M1.objects.filter(user = instance)
          m.user = ASSIGN_TO_ROOT_ON_DELETE
          m.save()
signals.pre_delete.connect(on_delete_user, sender = User)

####################
# Ap 2
class M1(models.Model):
  user = models.ForeignKey(User)
  very_transactional_stuff_two = models.CharFeld(max_length = 4)

def on_delete_user(sender, instance, *args, **kwargs):
     ASSIGN_TO_ROOT_ON_DELETE, created = User.object.get_or_create(username = 'UserDeleted')
     for m in M1.objects.filter(user = instance)
          m.user = ASSIGN_TO_ROOT_ON_DELETE
          m.save()

signals.pre_delete.connect(on_delete_user, sender = User)

Saddly this is what happens on a delete

  • all the 'dependant' objects are obtained


# db.models.base.py Line 423
        # Find all the objects than need to be deleted.
        seen_objs = CollectedObjects()
        self._collect_sub_objects(seen_objs)

        # Actually delete the objects.
        delete_objects(seen_objs)
  • pre_delete is called


# db.models.query Line 810
       # Pre-notify all instances to be deleted.
        for pk_val, instance in items:
            signals.pre_delete.send(sender=cls, instance=instance)
  • uses the objects in the first step and DELETED them all, even after we have just 'reassinged' them (as the initial filter matched)
# db.models.query Lines 810 - 830

I propose another signal that is the 'pre_prepare_for_delete' that slips into

# db.models.base.py Line 423
        #first fire off the prepared signal
        signals.pre_prepare_for_delete.send(sender=self.__class__, instance=self)
		
        # Find all the objects than need to be deleted.
        seen_objs = CollectedObjects()
        self._collect_sub_objects(seen_objs)

        # Actually delete the objects.
        delete_objects(seen_objs)

Thoughts?

Change History (5)

comment:1 by adamfast, 16 years ago

milestone: post-1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:3 by Johannes Dollinger, 15 years ago

Resolution: duplicate
Status: newclosed

While the requested feature is different, the use-case is addressed by #7539.

comment:4 by Johannes Dollinger, 15 years ago

Really a duplicate of #6108. (Related: #1007, #2288, #7539)

comment:5 by cgieringer, 14 years ago

Related: #6870

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