mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-23 22:49:50 +03:00
add Meta.extra_fields
option to ModelSerializer.
This commit is contained in:
parent
15c613a9eb
commit
de24d0b02c
|
@ -1103,6 +1103,7 @@ class ModelSerializer(Serializer):
|
||||||
"""
|
"""
|
||||||
fields = getattr(self.Meta, 'fields', None)
|
fields = getattr(self.Meta, 'fields', None)
|
||||||
exclude = getattr(self.Meta, 'exclude', None)
|
exclude = getattr(self.Meta, 'exclude', None)
|
||||||
|
extra_fields = getattr(self.Meta, 'extra_fields', None)
|
||||||
|
|
||||||
if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
|
if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
|
@ -1116,6 +1117,12 @@ class ModelSerializer(Serializer):
|
||||||
type(exclude).__name__
|
type(exclude).__name__
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if extra_fields and not isinstance(extra_fields, (list, tuple)):
|
||||||
|
raise TypeError(
|
||||||
|
'The `extra_fields` option must be a list or tuple. Got %s.' %
|
||||||
|
type(extra_fields).__name__
|
||||||
|
)
|
||||||
|
|
||||||
assert not (fields and exclude), (
|
assert not (fields and exclude), (
|
||||||
"Cannot set both 'fields' and 'exclude' options on "
|
"Cannot set both 'fields' and 'exclude' options on "
|
||||||
"serializer {serializer_class}.".format(
|
"serializer {serializer_class}.".format(
|
||||||
|
@ -1159,6 +1166,9 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
# Use the default set of field names if `Meta.fields` is not specified.
|
# Use the default set of field names if `Meta.fields` is not specified.
|
||||||
fields = self.get_default_field_names(declared_fields, info)
|
fields = self.get_default_field_names(declared_fields, info)
|
||||||
|
# Add extra field names, if any.
|
||||||
|
if extra_fields is not None:
|
||||||
|
fields += list(extra_fields)
|
||||||
|
|
||||||
if exclude is not None:
|
if exclude is not None:
|
||||||
# If `Meta.exclude` is included, then remove those fields.
|
# If `Meta.exclude` is included, then remove those fields.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user