Opened 16 years ago

Closed 16 years ago

#10186 closed (invalid)

"post_create" or "post_first_save" signal

Reported by: bmjames Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be useful to have a built-in signal emitted when a model instance is saved *for the first time*. For example, when I post a new article on my site I want to announce it via Twitter, but I don't want it announced again if I edit the article later.

There are some cases where using the pre_save signal, and checking if the id of the instance is None, is not sufficient. For example, if the URL of the article I want to announce on Twitter contains the article's ID, I can't announce the URL of the article because that ID is not yet known at the time of the pre_save signal.

By the time the post_save signal is emitted, I can't tell whether it was the first time the article was saved if I use only the built-in signals. I must check if the ID of the instance is None before it is saved, store that stateful information somewhere, then access that information on post_save when I am finally able to dispatch the announcement. But this is messy and, in my opinion, far too much of a logical dance for a simple requirement.

The only elegant way I have found is to create my own custom signal which is sent in the model's save() method:

    def save(self):
        first_save = False
        if self.id is None:
            first_save = True
        Model.save(self)
        if first_save:
            post_create.send(sender=Post, instance=self)

I believe this should be a common-enough case to warrant a built-in signal, rather than needing to override the save() method in many models.

Change History (1)

comment:1 by Erin Kelly, 16 years ago

Resolution: invalid
Status: newclosed

The post_save signal already provides this information via its created argument.

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