Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, how to override the 'Save and continue' feature?

I need to add some pre- and post-save logic to my ModelAdmin, but only when the user submitted the form via the 'Save and continue editing' button and not the 'Save' button. How can I do this?

like image 618
Dan Mantyla Avatar asked Dec 16 '22 15:12

Dan Mantyla


1 Answers

Just like overriding the normal save method, you need to override the save_model() function in your ModelAdmin, which includes the request object. From the request object you can get the POST object, which will include a '_continue' key if the user clicked the 'Save and continue button'. Example:

class MyAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, changed):
        if '_continue' in request.POST:
            # add your code here
        return super(ServerAdmin, self).change_view(request, obj, form, changed)
like image 158
Dan Mantyla Avatar answered Jan 08 '23 09:01

Dan Mantyla