Home > Blockchain >  Django REST Framework with ForeignKey on_delete=models.PROTECT
Django REST Framework with ForeignKey on_delete=models.PROTECT

Time:01-13

when I try to delete Django model object that is ForeignKey in another model with option on_delete=models.PROTECT, the error returned is the normal Django 500 Exception HTML web page, how to make Django rest frame work return json response with the error, is there a way for DRF to do that by default or it should be customized?

CodePudding user response:

Raising 500 Exception is the expected behavior. You have to customize to have a custom error. You can refer to this similar question

CodePudding user response:

You can catch the error by using ProtectedError expceptions. Like this-

from django.db.models import ProtectedError

try:
    # Write here your code
except ProtectedError:
    # Return your customer validation error

  •  Tags:  
  • Related