[Answered ]-Django REST and request.data MultiValueDictKeyError

2πŸ‘

βœ…

500s aren’t a good meaningful response. You should be validating just about everything. To keep it DRY you can create helper functions to solve the general problem. Something like:

def validate_request(req, keys):
    for key in keys:
        if key not in request.data:
            raise APIException('request.data missing key "{}"'.format(key))

Edit: You’ll have to handle the exception and return a validation error to wherever the request comes from. If you are trying to be RESTful, then you can use status codes with whatever message, or a JSON response with the validation error, or the like. Depends on your use case.

πŸ‘€Chris Jacobs

Leave a comment