I am trying to use the CurrentUserDefault class for a single serializer.
user = serializers.HiddenField(default=serializers.CurrentUserDefault())
The docs say:
To use this, a βqueryβ must be provided as part of the context dictionary when instantiating the serializer.
I am not sure how to create a serializer. In the view, I create all serializers with:
serializer = NewModelSerializer(data=request.data)
So I tried:
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 execute unicode with dictionary keys ( u'request' ) with the same luck.
Is there a better way to pass registered user to serializer?
I am using Django REST Framework 3.0 and Python 2.7.6
python django django-rest-framework
alfonso.kim
source share