Skip to content

Commit

Permalink
Fix translate_validation: use params (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored and carltongibson committed Dec 17, 2018
1 parent a6ffe8b commit b3c0b81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion django_filters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def translate_validation(error_dict):
from rest_framework.exceptions import ValidationError, ErrorDetail

exc = OrderedDict(
(key, [ErrorDetail(e.message, code=e.code) for e in error_list])
(key, [ErrorDetail(e.message % (e.params or ()), code=e.code)
for e in error_list])
for key, error_list in error_dict.as_data().items()
)

Expand Down
22 changes: 20 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from django_filters import FilterSet
from django_filters.exceptions import FieldLookupError
from django_filters.filters import MultipleChoiceFilter
from django_filters.utils import (
MigrationNotice,
RenameAttributesBase,
Expand Down Expand Up @@ -511,17 +512,30 @@ class Meta:
model = Article
fields = ['id', 'author', 'name']

choice = MultipleChoiceFilter(choices=[('1', 'one'), ('2', 'two')])

def test_error_detail(self):
f = self.F(data={'id': 'foo', 'author': 'bar', 'name': 'baz'})
f = self.F(data={
'id': 'foo',
'author': 'bar',
'name': 'baz',
'choice': ['3'],
})
exc = translate_validation(f.errors)

self.assertDictEqual(exc.detail, {
'id': ['Enter a number.'],
'author': ['Select a valid choice. That choice is not one of the available choices.'],
'choice': ['Select a valid choice. 3 is not one of the available choices.'],
})

def test_full_error_details(self):
f = self.F(data={'id': 'foo', 'author': 'bar', 'name': 'baz'})
f = self.F(data={
'id': 'foo',
'author': 'bar',
'name': 'baz',
'choice': ['3'],
})
exc = translate_validation(f.errors)

self.assertEqual(exc.get_full_details(), {
Expand All @@ -530,4 +544,8 @@ def test_full_error_details(self):
'message': 'Select a valid choice. That choice is not one of the available choices.',
'code': 'invalid_choice',
}],
'choice': [{
'message': 'Select a valid choice. 3 is not one of the available choices.',
'code': 'invalid_choice',
}],
})

0 comments on commit b3c0b81

Please sign in to comment.