Skip to content

Serializer missing context if pagination is used. #2355

Closed
@flc

Description

@flc

affects all versions of drf3; worked in drf2

suppose we have an app called 'text' with a models.py (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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions