How to pass database object id which initiated Django post save signal

I am trying to solve the following problem. I have django view which provide functionality to save object in database. After view will save the object i want to proccess some logic on saved object at once (e.g check similarity of some fields with another objects)
I hear about django signals specially about post_save signal which i think will appropriate for my usecase. But for my usecase i need to pass object id which initiated execution of post_save signal. Is in django signals any built-in solution to extract this object id to futher passing it to receiver of signal function
Hope will my pseudo code will give more ckarification

app_view(receive and save data as django model object) 

post_save signal(receiver, id_of_object_initiated_execution)

Upvotes: 1

Views: 1572

Answers (1)

Ogulcan Olguner
Ogulcan Olguner

Reputation: 581

You can use the post_save signal with a code like the one below. The 'instance' parameter represents the saved object. So 'instance.id' will give the id of the object.

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

@receiver(post_save, sender=<YourModel>)
def post_save_function(sender, instance, **kwargs):
    object_id = instance.id
    """
    the rest of the logic here
    """

Upvotes: 5

Related Questions