In Django admin Panel. I have a model which has fields like userID, name, status. I want to call an API (e.g:- www.xyx.com?user=userID&status='approved') when status="approved" is selected and click on save button.
You can override the save_model()
method in ModelAdmin
. From there, you can make the API request before delegating the save operation itself to the parent class.
For example, assuming your model class is called MyModel
, and that you are using the Requests library to call the API:
from django.contrib import admin
import requests
class MyModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if obj.status == 'approved':
# Make the API call
requests.get('http://www.example.com', params={'user': obj.userID, 'status': 'approved'})
# Delegate the save to the parent class
super().save_model(request, obj, form, change)