MCPcopy Index your code
hub / github.com/encode/django-rest-framework / get_field_names

Method get_field_names

rest_framework/serializers.py:1151–1241  ·  view source on GitHub ↗

Returns the list of all field names that should be created when instantiating this serializer class. This is based on the default set of fields, but also takes into account the `Meta.fields` or `Meta.exclude` options if they have been specified.

(self, declared_fields, info)

Source from the content-addressed store, hash-verified

1149 # Methods for determining the set of field names to include...
1150
1151 def get_field_names(self, declared_fields, info):
1152 """
1153 Returns the list of all field names that should be created when
1154 instantiating this serializer class. This is based on the default
1155 set of fields, but also takes into account the `Meta.fields` or
1156 `Meta.exclude` options if they have been specified.
1157 """
1158 fields = getattr(self.Meta, 'fields', None)
1159 exclude = getattr(self.Meta, 'exclude', None)
1160
1161 if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
1162 raise TypeError(
1163 'The `fields` option must be a list or tuple or "__all__". '
1164 'Got %s.' % type(fields).__name__
1165 )
1166
1167 if exclude and not isinstance(exclude, (list, tuple)):
1168 raise TypeError(
1169 'The `exclude` option must be a list or tuple. Got %s.' %
1170 type(exclude).__name__
1171 )
1172
1173 assert not (fields and exclude), (
1174 "Cannot set both 'fields' and 'exclude' options on "
1175 "serializer {serializer_class}.".format(
1176 serializer_class=self.__class__.__name__
1177 )
1178 )
1179
1180 assert not (fields is None and exclude is None), (
1181 "Creating a ModelSerializer without either the 'fields' attribute "
1182 "or the 'exclude' attribute has been deprecated since 3.3.0, "
1183 "and is now disallowed. Add an explicit fields = '__all__' to the "
1184 "{serializer_class} serializer.".format(
1185 serializer_class=self.__class__.__name__
1186 ),
1187 )
1188
1189 if fields == ALL_FIELDS:
1190 fields = None
1191
1192 if fields is not None:
1193 # Ensure that all declared fields have also been included in the
1194 # `Meta.fields` option.
1195
1196 # Do not require any fields that are declared in a parent class,
1197 # in order to allow serializer subclasses to only include
1198 # a subset of fields.
1199 required_field_names = set(declared_fields)
1200 for cls in self.__class__.__bases__:
1201 required_field_names -= set(getattr(cls, '_declared_fields', []))
1202
1203 for field_name in required_field_names:
1204 assert field_name in fields, (
1205 "The field '{field_name}' was declared on serializer "
1206 "{serializer_class}, but has not been included in the "
1207 "'fields' option.".format(
1208 field_name=field_name,

Callers 1

get_fieldsMethod · 0.95

Calls 1

Tested by

no test coverage detected