From 9ec2963be075aea95c8e6b7e677850f4f8ab15ca Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 22 May 2019 18:21:21 -0700 Subject: [PATCH 1/2] Test init for fields w/ lazy translations --- tests/importable/__init__.py | 17 ++++++++++++++++- tests/importable/test_installed.py | 17 ++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/tests/importable/__init__.py b/tests/importable/__init__.py index b365990278..ded08258c8 100644 --- a/tests/importable/__init__.py +++ b/tests/importable/__init__.py @@ -1 +1,16 @@ -from rest_framework import compat # noqa +""" +This test "app" exists to ensure that parts of Django REST Framework can be +imported/invoked before Django itself has been fully initialized. +""" + +from rest_framework import compat, serializers # noqa + + +# test initializing fields with lazy translations +class ExampleSerializer(serializers.Serializer): + charfield = serializers.CharField(min_length=1, max_length=2) + integerfield = serializers.IntegerField(min_value=1, max_value=2) + floatfield = serializers.FloatField(min_value=1, max_value=2) + decimalfield = serializers.DecimalField(max_digits=10, decimal_places=1, min_value=1, max_value=2) + durationfield = serializers.DurationField(min_value=1, max_value=2) + listfield = serializers.ListField(min_length=1, max_length=2) diff --git a/tests/importable/test_installed.py b/tests/importable/test_installed.py index 072d3b2e43..c7e53af233 100644 --- a/tests/importable/test_installed.py +++ b/tests/importable/test_installed.py @@ -4,10 +4,21 @@ def test_installed(): - # ensure that apps can freely import rest_framework.compat + # ensure the test app hasn't been removed from the test suite assert 'tests.importable' in settings.INSTALLED_APPS -def test_imported(): - # ensure that the __init__ hasn't been mucked with +def test_compat(): assert hasattr(importable, 'compat') + + +def test_serializer_fields_initialization(): + assert hasattr(importable, 'ExampleSerializer') + + serializer = importable.ExampleSerializer() + assert 'charfield' in serializer.fields + assert 'integerfield' in serializer.fields + assert 'floatfield' in serializer.fields + assert 'decimalfield' in serializer.fields + assert 'durationfield' in serializer.fields + assert 'listfield' in serializer.fields From 80692c0d9e5bebd277d4c9d41e23aa796fc50252 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 22 May 2019 18:24:34 -0700 Subject: [PATCH 2/2] Fix lazy translations for ListField --- rest_framework/fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 179dd25c89..2131347936 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1610,10 +1610,10 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.child.bind(field_name='', parent=self) if self.max_length is not None: - message = self.error_messages['max_length'].format(max_length=self.max_length) + message = lazy(self.error_messages['max_length'].format, str)(max_length=self.max_length) self.validators.append(MaxLengthValidator(self.max_length, message=message)) if self.min_length is not None: - message = self.error_messages['min_length'].format(min_length=self.min_length) + message = lazy(self.error_messages['min_length'].format, str)(min_length=self.min_length) self.validators.append(MinLengthValidator(self.min_length, message=message)) def get_value(self, dictionary):