From 92c630ef2417093f1e5cd04b6117e963fd011ddb Mon Sep 17 00:00:00 2001 From: Florian Demmer Date: Tue, 11 Nov 2014 14:27:42 +0100 Subject: [PATCH] add kwargs to get_serializer_context to add something to the context you can easily overload the get_serializer_context method. inside you have to call super to get the context, add your stuff and return it. it would save a little boilerplate if we would a take kwargs on the original method. that way the overloaded method is reduced to just the super call with the additional parameters for the context. instead of: def get_serializer_context(self): context = super(SuperClass, self).get_serializer_context() context.update(dict( example=True )) return context just do: def get_serializer_context(self): return super(SuperClass, self).get_serializer_context( example=True ) --- rest_framework/generics.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 3d6cf1684..862464b00 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -69,15 +69,17 @@ class GenericAPIView(views.APIView): # and should be considered private API. paginator_class = Paginator - def get_serializer_context(self): + def get_serializer_context(self, **kwargs): """ Extra context provided to the serializer class. """ - return { + context = { 'request': self.request, 'format': self.format_kwarg, 'view': self } + context.update(**kwargs) + return context def get_serializer(self, instance=None, data=None, many=False, partial=False): """