catch ForeignKey delete exception on city

This commit is contained in:
Kuroshini 2020-02-10 12:43:38 +03:00
parent dcf88408ae
commit f16adb23f7

View File

@ -1,5 +1,6 @@
"""Location app views."""
from django.contrib.postgres.fields.jsonb import KeyTextTransform
from django.db.models.deletion import ProtectedError
from rest_framework import generics, status
from rest_framework.response import Response
from rest_framework.views import APIView
@ -167,6 +168,13 @@ class CityRUDView(common.CityViewMixin, generics.RetrieveUpdateDestroyAPIView):
IsGuest,
)
def delete(self, request, *args, **kwargs):
try:
return self.destroy(request, *args, **kwargs)
except ProtectedError:
from rest_framework.serializers import ValidationError
raise ValidationError({'detail': 'City couldnt be deleted. Some objects has this city'})
# Region
class RegionListCreateView(common.RegionViewMixin, generics.ListCreateAPIView):