I'm trying to use the CurrentUserDefault
class for one serializer.
user = serializers.HiddenField(default=serializers.CurrentUserDefault())
The docs says:
In order to use this, the 'request' must have been provided as part of the context dictionary when instantiating the serializer.
I'm not sure how to create the serializer. On the view I create all the serializers with:
serializer = NewModelSerializer(data=request.data)
So I've attempted:
context = dict(request.data)
context['request'] = request
serializer = NewModelSerializer(data=context)
and
context['request'] = {'user': request.user}
And in both cases the error is the same:
Exception Type: KeyError
Exception Value: 'request'
on:
/Users/Alfonso/virtualenvs/sports/lib/python2.7/site-packages/rest_framework/fields.py in set_context
self.user = serializer_field.context['request'].user
Also I tried to unicode the keys of the dictionary (u'request'
) with same luck.
Is there a better way to pass the logged user to a serializer?
I'm using Django REST Framework 3.0 and Python 2.7.6