mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Merged v2.5.13
This commit is contained in:
@@ -4,7 +4,7 @@ import pytz
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import FieldError, MultipleObjectsReturned, ObjectDoesNotExist
|
||||
from django.db.models import ManyToManyField
|
||||
from django.db.models import ManyToManyField, ProtectedError
|
||||
from django.http import Http404
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework.permissions import BasePermission
|
||||
@@ -274,6 +274,19 @@ class ModelViewSet(_ModelViewSet):
|
||||
# Fall back to the hard-coded serializer class
|
||||
return self.serializer_class
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
try:
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
except ProtectedError as e:
|
||||
models = ['{} ({})'.format(o, o._meta) for o in e.protected_objects.all()]
|
||||
msg = 'Unable to delete object. The following dependent objects were found: {}'.format(', '.join(models))
|
||||
return self.finalize_response(
|
||||
request,
|
||||
Response({'detail': msg}, status=409),
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def list(self, *args, **kwargs):
|
||||
"""
|
||||
Call to super to allow for caching
|
||||
|
||||
Reference in New Issue
Block a user