Django: how can I tell if the post_save signal triggers on a new object?
Asked Answered
M

3

62

I need to do some background post-processing on newly created objects in Django. This post-processing should only run on new objects, not objects that are just updated.

I know that in pre_save I can check if the object has an id, if it has not then it's a new object. But the problem is that in the post-processing I need access to the id (so that I can save the results back to the database).

How can I do this in a clean way?

Mondrian answered 20/5, 2012 at 10:16 Comment(1)
Is it at all possible to do the processing after the actual save? Then use post_save, docs.djangoproject.com/en/dev/ref/signals/…. It has a boolean to say whether it's new or just an update.Feature
T
98

Have a look at docs: https://docs.djangoproject.com/en/stable/ref/signals/#post-save

There is a created named argument which will be set to True if it's a new object.

Timeworn answered 20/5, 2012 at 10:58 Comment(0)
A
30

As Docs stated and @seler pointed out, but with an example:

def keep_track_save(sender, instance, created, **kwargs):
    action = 'save' if created else 'update'
    save_duplicate((instance.id, instance.__class__.__name__, action))

post_save.connect(keep_track_save, sender=Group)
Abad answered 2/6, 2015 at 14:17 Comment(0)
G
13

I just leave it here, maybe it'll be helpful for someone.

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver


class Deal(models.Model):
    name = models.CharField(max_length=255)


@receiver(post_save, sender=Deal)
def print_only_after_deal_created(sender, instance, created, **kwargs):
    if created:
        print(f'New deal with pk: {instance.pk} was created.')
Goddaughter answered 18/2, 2019 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.