Search code examples
pythondjangodjango-admindjango-admin-toolsdjango-admin-actions

How to add a function after a record inserted in database using django admin?


I want to execute a function after a record inserted to database using django-admin panel .

I have a product table , i want to send notification to users when a record inserted in database by django admin panel . i know how to send notification to users , but i dont know where to put my code .

any suggestion will be helpfull .

How can i execute a function to notify user after my record inserted ?

here is my code to execute after record inserted :

from fcm_django.models import FCMDevice

device = FCMDevice.objects.all()

device.send_message(title="Title", body="Message", icon=..., data={"test": "test"})

i searched alot but not found anything usefull .

thanks to this great communtiy .


Solution

  • You can modify save method on your product model

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        device = FCMDevice.objects.all()
        device.send_message(title="Title", body="Message", icon=..., data={"test": "test"})
    

    Well, it will send the notification every time the instance is saved (not only when it was added in django admin panel).