Как изменить код состояния, получаемый в ответе в случае определенных исключений в Django rest framework

I am adding some details to my Django model from a react frontend. Everything is working fine and the API request is made with Axios. The data that is being submitted is an OneToOne Field with the user and hence submitting more than once raises an Integrity error of Unique Constraint. But the response I receive back has a status of 200. This triggers a notification in my frontend that says details submitted every time the button is pressed. However. that is not being submitted the second time because of the Integrity Error.

Мой вопрос заключается в том, если я обрабатываю исключение Integrity Error отдельно, как я могу отправить другой статус, а не 200, как показано ниже

config: {transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}
data: {message: 'UNIQUE constraint failed: teacher_teacherdetail.user_id'}
headers: {content-length: '39', content-type: 'application/json'}
request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: "OK"

В настоящее время я обрабатываю исключение следующим образом

try:
 # some code
    
    except IntegrityError as e:
      
      message = {
       "error": str(e)
      }
      
      return Response(message)
    
    
    except Exception as e:
      
      message = {
        "message": "exception",
        "error": str(e)
      }
      
      return Response(message)

Вам необходимо указать ошибку. Здесь вы можете найти все допустимые переменные

from rest_framework import status

message = {
    "error": str(e)
}
      
return Response(message, status=status.HTTP_400_BAD_REQUEST)
Вернуться на верх