Code

Opened 4 years ago

Closed 3 years ago

#13772 closed (wontfix)

'exists' parameter for pre_save signal

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

Description

As Model.save() is called both for inserts and updates, it would sometimes be useful for pre_save listeners to know whether the instance exists or not. This information is sent with post_save but by then it may be too late (e.g. under some conditions you may want to prevent an insertion from happening).

Attachments (2)

pre_save.patch (6.0 KB) - added by gsakkis 4 years ago.
Patch against r13861
13772.patch (6.9 KB) - added by gsakkis 4 years ago.
Patch against r13975

Download all attachments as: .zip

Change History (11)

comment:1 Changed 4 years ago by gsakkis

  • Has patch set
  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset

Patch (including updated tests) attached; needs docs, will add if accepted.

comment:2 Changed 4 years ago by gsakkis

  • Cc george.sakkis@… added
  • Component changed from Uncategorized to Database layer (models, ORM)
  • milestone set to 1.3

comment:3 Changed 4 years ago by ptone

  • Resolution set to worksforme
  • Status changed from new to closed
  • Triage Stage changed from Unreviewed to Accepted

The presave signal sends the instance of the object being saved. An unsaved instance will not yet have a pk, while an instance being updated will already have a pk.

Checking for a primary key on the instance will tell you whether the instance exists or not.

def my_callback(sender, instance=None, **kwargs):
    print "presave signal"
    if not instance.pk:
        print "not yet saved"
    else:
        print "exists, updating"

comment:4 Changed 4 years ago by gsakkis

  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Accepted to Unreviewed

Sorry, this works only if the pk field is AutoIncrement and the pk has not been set explicitly beforehand (e.g. with obj = MyModel(pk=42, ...) or obj.pk = 42).

comment:5 follow-up: Changed 4 years ago by ikelly

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

The patch moves the sending of the signal to after the recursive save_base call. This means that when the signal is sent, any parent models will have already been saved, which is undesirable. The patch should keep the signal where it is relative to the recursion, if at all possible.

Changed 4 years ago by gsakkis

Patch against r13861

comment:6 in reply to: ↑ 5 Changed 4 years ago by gsakkis

  • Patch needs improvement unset

The patch moves the sending of the signal to after the recursive save_base call. This means that when the signal is sent, any parent models will have already been saved, which is undesirable. The patch should keep the signal where it is relative to the recursion, if at all possible.

Updated according to suggestion and brought up to date with trunk (r13861).

Changed 4 years ago by gsakkis

Patch against r13975

comment:7 Changed 4 years ago by gsakkis

  • Cc gsakkis added; george.sakkis@… removed
  • Triage Stage changed from Accepted to Ready for checkin

Extended and migrated the doctests into proper unit tests.

comment:8 Changed 4 years ago by lrekucki

  • Triage Stage changed from Ready for checkin to Design decision needed

This ticket's history is a mess right now. You shouldn't mark your own patches as RFC. For starters, it still needs documentation. After that, they need to be reviewed by someone else.

Also, based on comment from Jeremy Dunck on django-dev, I'm not so sure we can just do this without cost. I'm going to push this back to DDN.

comment:9 Changed 3 years ago by gsakkis

  • milestone 1.3 deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed

Withdrawn after two negative and zero positive votes at http://groups.google.com/group/django-developers/browse_frm/thread/0c0dec77aea5d83a.

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.