Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST Framework - CurrentUserDefault use

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

like image 358
alfonso.kim Avatar asked Dec 24 '14 06:12

alfonso.kim


People also ask

What is Django REST framework used for?

Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.

Is Django REST framework needed?

Django REST Framework is only necessary if you're building a RESTful API; An HTTP service that reads and writes data, usually as JSON payloads. Services are typically created to allow external clients such as mobile apps, single page applications (React, Angular, etc.) or 3rd parties to gain access to your data.

Which Django use REST framework?

337 companies reportedly use Django REST framework in their tech stacks, including Robinhood, UpstageAI, and BirdView.

Why serializers are used in Django?

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.


1 Answers

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.

like image 97
Kevin Brown-Silva Avatar answered Oct 15 '22 12:10

Kevin Brown-Silva