Code

Opened 6 years ago

Closed 4 years ago

Last modified 4 years ago

#8168 closed (duplicate)

Need another Pre Delete Signal Type (feature)

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

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?

Attachments (0)

Change History (5)

comment:1 Changed 6 years ago by adamfast

  • milestone set to post-1.0
  • 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 anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:3 Changed 4 years ago by emulbreh

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

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

comment:4 Changed 4 years ago by emulbreh

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

comment:5 Changed 4 years ago by cgieringer

Related: #6870

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.