Reputation: 125
I Have a complex model with 6 constrains and i want to get a message to the user which constrain failed when the post-request fails.
class testView(APIView):
@staticmethod
def post(request):
serializer = testSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
try:
testModel.objects.create(
# all the data
)
return testView.get(request)
except IntegrityError:
return Response(status=status.HTTP_403_FORBIDDEN)
The exception is called but I cant find the specific constrain that faild in the IntegrityError
class.
Is there any way to return the specific Constrain to the User?
(iam using django 3.0.2 with Postgresql)
Upvotes: 1
Views: 2529