From 0b6b4134c7e25c90e12cf907df12123300d79245 Mon Sep 17 00:00:00 2001 From: afuenzalida Date: Sun, 29 May 2022 18:15:23 -0400 Subject: [PATCH] fix string format and check mispelling only on read_only_fields --- rest_framework/serializers.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index de1b2cbc4..90cb3e9ae 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1386,21 +1386,22 @@ class ModelSerializer(Serializer): if fields is not None: if not isinstance(fields, (list, tuple)): raise TypeError( - f'The `{option}` option must be a list or tuple. ' - f'Got {type(fields).__name__}.' + 'The `%s` option must be a list or tuple. ' + 'Got %s.' % (option, type(fields).__name__) ) for field_name in fields: kwargs = extra_kwargs.get(field_name, {}) kwargs[limit] = True extra_kwargs[field_name] = kwargs else: - # Guard against the possible misspelling `readonly_fields` (used - # by the Django admin and others). - assert not hasattr(self.Meta, 'readonly_fields'), ( - 'Serializer `%s.%s` has field `readonly_fields`; ' - 'the correct spelling for the option is `read_only_fields`.' % - (self.__class__.__module__, self.__class__.__name__) - ) + if option == READ_ONLY_FIELDS: + # Guard against the possible misspelling `readonly_fields` (used + # by the Django admin and others). + assert not hasattr(self.Meta, 'readonly_fields'), ( + 'Serializer `%s.%s` has field `readonly_fields`; ' + 'the correct spelling for the option is `read_only_fields`.' % + (self.__class__.__module__, self.__class__.__name__) + ) return extra_kwargs