Is there a way to validate params that are passed with the request without writing boilerplate code? Now I've got something like this:
project_id = kwargs['project_id']
try:
project_obj = Project.objects.get(id=project_id)
except Project.DoesNotExist:
return Response(
{'message': 'Requested project does not exist'},
status=status.HTTP_404_NOT_FOUND
)
except ValueError:
return Response(
{'message': 'Project id must be a number'},
status=status.HTTP_400_BAD_REQUEST
)
I've read about Serializers' Validation but I'm not sure it's the right thing. Without handling these exceptions, Django just returns 500, it's not the behavior I actually want.
project_ids and see if your ID passed in is in that list. To the other error you are catching,ValueError, you can write a simple check to see ifproject_idis a number and if not simply send back an error of your choice instead of catching the exception.