2015-11-18 48 views
1

我正在使用django rest框架。我需要傳遞一些額外的上下文值作爲響應,但不會得到extra_value的響應。包括額外的上下文值django rest框架

class ResultRowView(generics.ListAPIView): 
    serializer_class = ResultRowSerializer 
    permission_classes = (AccountPermission,) 

    def get_serializer(self, *args, **kwargs): 
     context = {'extra_value': 5000} 
     return self.serializer_class(*args, context=context, **kwargs) 

    def get_queryset(self): 
     qs = ResultRow.objects.none() 
     pk = self.kwargs.get('pk', None) 
     try: 
      route = IncomingRoute.objects.get(account=self.request.user.account, pk=pk) 
      qs = route.app_module.rows.all() 
     except Exception, e: 
      print 'result_row_query: ', e 
     return qs 

這裏缺少什麼?

+0

什麼是''extra_value? – utkbansal

+0

我需要傳遞一個額外的上下文值,並假設它的值是5000. –

回答

3

,你可以這樣做:

def list(self, request, *args, **kwargs): 
    response = super(ResultRowView, self).list(request, args, kwargs) 
    response.data[ 'extra_value' ] = 5000 
    return response 
0

你將不得不重寫get_context_data方法,然後通過重寫像列表的方法添加上下文它

def get_context_data(self, **kwargs): 
    context = super(PropertyListView, self).get_context_data(**kwargs) 
    context['some_key'] = some_value 
    return context 
+1

listAPIView中沒有get_context_data –