fix string format and check mispelling only on read_only_fields

This commit is contained in:
afuenzalida 2022-05-29 18:15:23 -04:00
parent 729b6ca188
commit 0b6b4134c7

View File

@ -1386,21 +1386,22 @@ class ModelSerializer(Serializer):
if fields is not None: if fields is not None:
if not isinstance(fields, (list, tuple)): if not isinstance(fields, (list, tuple)):
raise TypeError( raise TypeError(
f'The `{option}` option must be a list or tuple. ' 'The `%s` option must be a list or tuple. '
f'Got {type(fields).__name__}.' 'Got %s.' % (option, type(fields).__name__)
) )
for field_name in fields: for field_name in fields:
kwargs = extra_kwargs.get(field_name, {}) kwargs = extra_kwargs.get(field_name, {})
kwargs[limit] = True kwargs[limit] = True
extra_kwargs[field_name] = kwargs extra_kwargs[field_name] = kwargs
else: else:
# Guard against the possible misspelling `readonly_fields` (used if option == READ_ONLY_FIELDS:
# by the Django admin and others). # Guard against the possible misspelling `readonly_fields` (used
assert not hasattr(self.Meta, 'readonly_fields'), ( # by the Django admin and others).
'Serializer `%s.%s` has field `readonly_fields`; ' assert not hasattr(self.Meta, 'readonly_fields'), (
'the correct spelling for the option is `read_only_fields`.' % 'Serializer `%s.%s` has field `readonly_fields`; '
(self.__class__.__module__, self.__class__.__name__) 'the correct spelling for the option is `read_only_fields`.' %
) (self.__class__.__module__, self.__class__.__name__)
)
return extra_kwargs return extra_kwargs