Skip to content

Instantly share code, notes, and snippets.

@aleksb86
Created October 13, 2018 16:01
Show Gist options
  • Save aleksb86/b199a0eaf99475a3b8602ec0d7b94b9d to your computer and use it in GitHub Desktop.
Save aleksb86/b199a0eaf99475a3b8602ec0d7b94b9d to your computer and use it in GitHub Desktop.
def validate_request_data(fields, model_name):
def decorator_method(fn):
def decorated(*args, **kwargs):
# args[0] == GenericView Object
error_messages = []
for field in fields:
if not args[0].request.data.get(field, None):
error_messages.append("Field '{}' is required for {}".format(field, model_name))
return Response(
data={
"message": ', '.join(error_messages)
},
status=status.HTTP_400_BAD_REQUEST
)
return fn(*args, **kwargs)
return decorated
return decorator_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment