Friday, May 27, 2011

Perform Extra Operations after Saving All Inlines in Django Admin

Django Admin provides quite solid foundation for performing CRUD operations. Djando Admin does most things already.
What I needed to do this time is to perform extra operations after saving all inlines in Django Admin. If I do not need to access request object, I could perform extra operations by overriding save method. The thing is I need to get current logged in user, that is exactly why I need to do it from admin.ModelAdmin.

First attempt is by overriding admin.ModelAdmin save_model method. Failed, due to after performing, the inline data are not yet available.

What next? Fortunately, I found helpful information from this link.

From that, we could intercept right before preparing the response.
There are three methods which related with setting up response:
response_add, response_change, response_action.

Here is an example of overriding response_add method.
def response_add(self, request, new_object):
    # Perform extra operation after all inlines are saved
    return super(AdminSubClass, self).response_add(request, new_object)

There is a ticket with this issue.
Hopefully there will be even better way to perform such action.

No comments:

Post a Comment