diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index fee8f024f..98ca49e9f 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -142,7 +142,7 @@ class ValidationError(APIException): default_detail = _('Invalid input.') default_code = 'invalid' - def __init__(self, detail=None, code=None): + def __init__(self, detail=None, code=None, params=None): if detail is None: detail = self.default_detail if code is None: @@ -151,9 +151,18 @@ class ValidationError(APIException): # For validation failures, we may collect many errors together, # so the details should always be coerced to a list if not already. if isinstance(detail, tuple): - detail = list(detail) + detail_list = list() + if params is not None: + for msg in detail: + detail_list.append(msg % params) + detail = detail_list + else: + detail = list(detail) elif not isinstance(detail, dict) and not isinstance(detail, list): - detail = [detail] + if params is not None: + detail = [detail % params] + else: + detail = [detail] self.detail = _get_error_details(detail, code) diff --git a/tests/test_validation_error.py b/tests/test_validation_error.py index 341c4342a..537506771 100644 --- a/tests/test_validation_error.py +++ b/tests/test_validation_error.py @@ -109,3 +109,16 @@ class TestValidationErrorConvertsTuplesToLists(TestCase): assert len(error.detail) == 2 assert str(error.detail[0]) == 'message1' assert str(error.detail[1]) == 'message2' + +class TestvalidationErrorWithDjangoStyle(TestCase): + def test_validation_error_details(self): + error = ValidationError('Invalid value: %(value)s', params={'value': '42'}) + s = str(error.detail) + assert str(error.detail[0]) == 'Invalid value: 42' + + def test_validation_error_details_tuple(self): + error = ValidationError(detail=('Invalid value: %(value1)s', 'Invalid value: %(value2)s'), params={'value1': '42', 'value2':'43'}) + assert isinstance(error.detail, list) + assert len(error.detail) == 2 + assert str(error.detail[0]) == 'Invalid value: 42' + assert str(error.detail[1]) == 'Invalid value: 43' \ No newline at end of file