Reputation: 2900
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
Upvotes: 13
Views: 9900
Reputation: 41671
Django REST Framework handles the serialization and deserialization of objects using a central serializer. In order to help deserialize and serialize sometimes, it needs a bit of context like the current view
or request
that is being used. You typically don't have to worry about it because the generic views handle it automatically for you. This is covered in the documentation under "Including extra context" and it takes advantage of the optional context
parameter for serializers.
When you are using serializers manually, the context must be passed in as a dictionary. Some fields require specific keys, but for the most part you only need the request
key to be a reference to the incoming request. This will allow the HyperlinkedRelatedField
to generate a full URL, and extras like the CurrentUserDefault
to perform as expected.
context = {
"request": self.request,
}
serializer = NewModelSerializer(data=request.data, context=context)
The context dictionary is also available on generic views as the get_serializer_context
method, which will automatically populate the dictionary with commonly used keys such as the request
and view
.
Upvotes: 29