1π
β
In both Django and in Django Rest Framework these type of things are dealt with by raising Exceptions:
from rest_framework.exceptions import APIException
class BadParameter(APIException):
status_code = 400
default_detail = 'The parameter x is wrong or empty'
default_code = 'bad_param'
# ...
class PlantListByClientView(generics.ListAPIView):
# ...
def list(self, request, *args, **kwargs):
# ...
if bad_param:
raise BadParameter()
Some parts of the framework will raise exceptions for you, like, for example, serializes rising ValidationError
when validation fails.
π€Igonato
Source:stackexchange.com