Opened 14 years ago
Closed 14 years ago
#13772 closed (wontfix)
'exists' parameter for pre_save signal
Reported by: | George Sakkis | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Keywords: | ||
Cc: | George Sakkis | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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)
Change History (11)
comment:1 by , 14 years ago
Has patch: | set |
---|---|
Needs documentation: | set |
comment:2 by , 14 years ago
Cc: | added |
---|---|
Component: | Uncategorized → Database layer (models, ORM) |
milestone: | → 1.3 |
comment:3 by , 14 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
Triage Stage: | Unreviewed → 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 by , 14 years ago
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
Triage Stage: | Accepted → 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
).
follow-up: 6 comment:5 by , 14 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → 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.
comment:6 by , 14 years ago
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).
comment:7 by , 14 years ago
Cc: | added; removed |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Extended and migrated the doctests into proper unit tests.
comment:8 by , 14 years ago
Triage Stage: | Ready for checkin → 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 by , 14 years ago
milestone: | 1.3 |
---|---|
Resolution: | → wontfix |
Status: | reopened → closed |
Withdrawn after two negative and zero positive votes at http://groups.google.com/group/django-developers/browse_frm/thread/0c0dec77aea5d83a.
Patch (including updated tests) attached; needs docs, will add if accepted.