Support usage of 'source' in extra_kwargs
. by theosotr · Pull Request #4688 · encode/django-rest-framework (original) (raw)
Imagine the following scenario:
class MyModel(models.Model): model_field_name = models.CharField(max_length=255)
class MySerializer(serializers.ModelSerializer): class Meta(object): fields = ['api_field_name'] extra_kwargs = { 'api_field_name': { 'source': 'model_field_name' } } model = models.MyModel
This code will break down with the following exception.
Traceback (most recent call last):
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 87, in view
return self.dispatch(request, *args, **kwargs)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/views.py", line 474, in dispatch
response = self.handle_exception(exc)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/views.py", line 434, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/views.py", line 471, in dispatch
response = handler(request, *args, **kwargs)
File "/home/thodoris/projects/apimas/apimas/modeling/adapters/drf/mixins.py", line 116, in list
serializer = self.get_serializer(queryset, many=True)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/generics.py", line 111, in get_serializer
return serializer_class(*args, **kwargs)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 102, in __new__
return cls.many_init(*args, **kwargs)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 123, in many_init
child_serializer = cls(*args, **kwargs)
File "/home/thodoris/projects/apimas/apimas/modeling/adapters/drf/serializers.py", line 24, in __init__
serializer_fields = self.fields
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 340, in fields
for key, value in self.get_fields().items():
File "/home/thodoris/projects/apimas/apimas/modeling/adapters/drf/serializers.py", line 100, in get_fields
field_name, info, model, depth
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 1104, in build_field
return self.build_unknown_field(field_name, model_class)
File "/home/thodoris/projects/apimas/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 1211, in build_unknown_field
(field_name, model_class.__name__)
ImproperlyConfigured: Field name `api_field_name` is not valid for model `MyModel`.
My commit fixes this issue so that I will no longer need to specify the api_field_name
explicitly as follows:
class MySerializer(serializers.ModelSerializer): api_field_name = serializers.CharField(source='model_field_name') class Meta(object): fields = ['api_field_name'] model = models.MyModel