0

I've overridden my save_model() function to wrap the obj.save() call in a try/catch.

def save_model(self, request, obj, form, change):
    from concurrency.exceptions import RecordModifiedError
    from django.http import HttpResponse
    try: 

        obj.save()
        # some other stuff

    except RecordModifiedError:
        messages.error(request, "[!] Record modified. Please try again.")
        #self.message_user(request, "[!] Record modified. Please try again.", level="error")

Catching the RecordModifiedError is working, and the data is not saved. However, the confirmation message that appears on a successful save is still showing, as is the error. I have two contradictory messages being shown!

I'm wondering how to prevent the success message from being displayed. Thanks!

EDIT: also tried the self.message_user() function, but it didn't block the success message either.

iank
  • 800
  • 3
  • 10
  • 32

3 Answers3

0

doesnot it have to be in this way?

try: 

    obj.save()
    messages.error(request, "[!] Record modified.")

except RecordModifiedError:
    messages.error(request, "[!] Record NOT modified. Please try again.")
doniyor
  • 36,596
  • 57
  • 175
  • 260
  • This didn't change the behaviour. Now I see two success messages when I save successfully. Thanks though. – iank Nov 26 '14 at 19:15
0

I think this should work:

storage = messages.get_messages(request)
storage.used = True
messages.error(request, "[!] Record modified. Please try again.")
dan-klasson
  • 13,734
  • 14
  • 63
  • 101
0

I solved this same issue by overwriting the message_user function. I set a flag if there was an error. If the flag is set, then have message_user return, if not set then call the base class message_user using the super function.

Adrian W
  • 4,563
  • 11
  • 38
  • 52
Mark
  • 1
  • 1