solo1977
solo1977

Reputation: 393

Django Admin - prevent save under certain conditions and display message to user

I am putting together a Django app - created and registered the models and model forms.

When adding or editing a particular model record, I need to call an external service for some information. If the information is retrieved successfully I want to update a column on that model which the user is unaware of and save the model. If the call fails I need to prevent the save from occurring and a custom message returned to the user.

I have tried raising Exceptions, returning messages as String etc but nothing seems to work. Although I can stop the save from occurring I end up with a stack-trace if I don't return the model.

I have seen various solutions using the clean() method or signals but none seem to suit the need.

The order of things I need to happen:

  1. Validate the Form data being entered (happens automatically)

  2. Do the API call to the external service to retrieve the info

  3. Assign the info to the model prop and save the model

  4. If API call fails, cancel save() and display message to user telling them the save has failed with a reason why. If the save fails then the page should reload and not be redirected to the listing page.

I just cannot seem to find a simple solution out there - any help appreciated!

Upvotes: 4

Views: 3353

Answers (3)

gkaravo
gkaravo

Reputation: 163

It seems to me that a successful API call is part of the validation process. According to this point of view, you can just check the API connection in clean() and throw a yourForm.add_error(None, "sometext") that will appear on the form if this goes wrong.

You can then proceed overriding yourModelAdmin.save_model(), including a new API call that will get and process the data this time, with confidence that everything will work as expected.

Keep in mind that according to the documentation here, ModelAdmin.save_model() should not be used to prevent saving the object.

Upvotes: 1

Sawan Chauhan
Sawan Chauhan

Reputation: 791

from django.contrib import messages

class MyAdminView(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        try:
            super(MyAdminView, self).save_model(request, obj, form, change)
        except Exception as e:
            messages.set_level(request, messages.ERROR)
            messages.error(request, e)

Upvotes: 2

Iakovos Belonias
Iakovos Belonias

Reputation: 1373

You can overwrite save_model of ModelAdmin.

  class MyAdminView(admin.ModelAdmin):
       def save_model(self, request, obj, form, change):
           super(MyAdminView, self).save_model(request, obj, form, change)

Upvotes: 1

Related Questions