13

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

1 Answer 1

29

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.

2
  • 14
    You sir saved my christmas Dec 24, 2014 at 18:46
  • 2
    This is very helpful. And unfortunately the error it throws if you don't supply the extra context is not very helpful at all. It throws a KeyError exception with the message u'request'.
    – Emeka
    Jan 11, 2015 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.