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:
Validate the Form data being entered (happens automatically)
Do the API call to the external service to retrieve the info
Assign the info to the model prop and save the model
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!