Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer missing context if pagination is used. #2355

flc opened this issue Dec 25, 2014 · 3 comments

Serializer missing context if pagination is used. #2355

flc opened this issue Dec 25, 2014 · 3 comments


Copy link

flc commented Dec 25, 2014

affects all versions of drf3; worked in drf2

suppose we have an app called 'text' with a (i've just put everything there for simplicity):

from django.db import models
from rest_framework import viewsets, serializers
from rest_framework.test import APITestCase

class Text(models.Model):
   text = models.TextField()

class TextSerializer(serializers.ModelSerializer):

   def __init__(self, *args, **kwargs):
      super(TextSerializer, self).__init__(*args, **kwargs)
      print 'Context in serializer', self.context

class TextViewSet(viewsets.ModelViewSet):
    queryset = Text.objects.all()
    serializer_class = TextSerializer

class TextViewSetWithPagination(TextModelViewSet):
    paginate_by = 10

opening a shell:

In [1]: from rest_framework.test import APIRequestFactory
In [2]: from text.models import *
In [3]: TextViewSet.as_view({'get':'list'})(APIRequestFactory().get('/'))
Context in serializer {u'view': <text.views.TextViewSet object at 0x5fba390>, u'request': <rest_framework.request.Request object at 0x5fba650>, u'format': None}
Out[3]: <rest_framework.response.Response at 0x5fdb2d0>
In [4]: TextViewSetWithPagination.as_view({'get':'list'})(APIRequestFactory().get('/'))
Context in serializer {}
Out[4]: <rest_framework.response.Response at 0x7fe8dd0>
zhanglc added a commit to zhanglc/django-rest-framework that referenced this issue Dec 26, 2014
@tomchristie tomchristie added this to the 3.0.3 Release milestone Dec 26, 2014
@tomchristie tomchristie changed the title missing default serializer context if pagination is used in generic view Serializer missing context if pagination is used. Dec 26, 2014
Copy link

Thanks for the report.
Fixed by c2e00a0
Incoming for the 3.0.3 release.

Copy link

Reopening for re-assessment based on this comment...

the context still {} or None, when the object_serializer() init

@tomchristie tomchristie reopened this Jan 5, 2015
Copy link

vplme commented Jan 5, 2015

object_serializer() is instantiated in the BasePaginationSerializer which calls the def __init__ of the object_serializer (in this case TextSerializer.__init__)
Since bind() is called after instantiation of object_serializer the context is not yet available in object_serializer.__init__.

Maybe the context needs to be passed directly to object_serializer? (e.g. object_serializer(context=self.context)).
generics.GenericAPIView.get_serializer() seems to do the same and also passes the context as a parameter (in kwargs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants