Exception Value:
("Cannot delete some instances of model 'GuestContact' because they are referenced through a protected foreign key: 'Reservation.res_company'", <QuerySet [<Reservation: Reservation object>, <Reservation: Reservation object>]>)
我没有&#39;想看看django错误页面。我想发送信息&#34;此记录无法删除,但此联系人用于预订&#34;
你可以帮帮我吗?
问候
答案 0 :(得分:2)
您可以在delete()
中使用try:except
并返回带有自定义错误消息的回复,
from django.db.models import ProtectedError
try:
instance.delete()
except ProtectedError:
error_message = "This object can't be deleted!!"
return JsonResponse(error_message)
答案 1 :(得分:1)
您好,您需要使用例外ProtectedError
try:
#DELETE STUFF
except ProtectedError:
#CUSTOM MESSAGE
引发以防止在使用时删除引用的对象 django.db.models.PROTECT。 models.ProtectedError是的子类 IntegrityError。
doc here:https://docs.djangoproject.com/fr/1.11/ref/exceptions/#django.db.models.ProtectedError