title: django rest framework create json and csv api
body: <p>i am trying to create 2 api's. 1 to access json using ajax datatable, and the other to create an export csv button. </p>
  
  <p>the json api i manage to create and get the data from it using ajax, but i get problems when i try to access the api in the browser and the other to add the csv api..i will show you part of my scripts and the errors i get.</p>
  
  <p>views.py</p>
  
  <pre><code>class VideoViewSet(viewsets.ModelViewSet):
      queryset = DatamsVideos.objects.all()
      serializer_class = VideosSerializer
  
      parser_classes = (CSVParser,) + tuple(api_settings.DEFAULT_PARSER_CLASSES)
      renderer_classes = (CSVRenderer,) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
  
      def list(self, request, **kwargs):
          try:
              vs = query_vs_by_args(**request.query_params)
              serializer = VideosSerializer(vs['items'], many=True)
              result = dict()
              result['data'] = serializer.data
              result['draw'] = vs['draw']
              result['recordsTotal'] = vs['total']
              result['recordsFiltered'] = vs['count']
              return Response(result, status=status.HTTP_200_OK, template_name=None, content_type=None)
  
          except Exception as e:
              return Response(e, status=status.HTTP_404_NOT_FOUND, template_name=None, content_type=None)
  
      def get_renderer_context(self):
          context = super(VideoViewSet, self).get_renderer_context()
          context['header'] = (
              self.request.GET['fields'].split(',')
              if 'fields' in self.request.GET else None)
          return context
  
      @action(methods=['GET'], detail=False)
      def bulk_upload(self, request, *args, **kwargs):
          serializer = self.get_serializer(data=request.data, many=True)
          serializer.is_valid(raise_exception=True)
          serializer.save()
  
          return Response(serializer.data, status=status.HTTP_303_SEE_OTHER, headers={'Location': reverse('videos-api')})
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>from rest_framework.routers import DefaultRouter
  
  router = DefaultRouter()
  router.register(r'videos', VideoViewSet)
  
  urlpatterns = [
      url(r'^$', index, name='main'),
      url(r'^api/', include(router.urls)),
  ]
  </code></pre>
  
  <p>and this is part of my models.py</p>
  
  <pre><code>def query_vs_by_args(**kwargs):
      draw = int(kwargs.get('draw', None)[0])
      length = int(kwargs.get('length', None)[0])
      start = int(kwargs.get('start', None)[0])
      search_value = kwargs.get('search[value]', None)[0]
      order_column = kwargs.get('order[0][column]', None)[0]
      order = kwargs.get('order[0][dir]', None)[0]
  
      order_column = ORDER_VS_COLUMN_CHOICES[order_column]
      # django orm '-' -&gt; desc
      if order == 'desc':
          order_column = '-' + order_column
  
      queryset = DatamsVideos.objects.all()
      total = queryset.count()
  
      if search_value:
          queryset = queryset.filter(Q(link__icontains=search_value) |
                                     Q(title__icontains=search_value) |
                                     Q(views__icontains=search_value) |      Q(date_added__icontains=search_value))                                         
  
      count = queryset.count()
      queryset = queryset.order_by(order_column)[start:start + length]
      return {
          'items': queryset,
          'count': count,
          'total': total,
          'draw': draw
      }
  </code></pre>
  
  <p>1 of the problem i have is with the function <strong>list</strong> from <strong>views.py</strong> at </p>
  
  <pre><code>vs = query_vs_by_args(**request.query_params)
  serializer = VideosSerializer(vs['items'], many=True)
  </code></pre>
  
  <p>i still manage to get the data from it using ajax, but when i try to access the api on the browser <strong>/main/api/videos/</strong> it gives me this error:</p>
  
  <pre><code>   HTTP 404 Not Found
  Allow: GET, POST, HEAD, OPTIONS
  Content-Type: text/csv ;utf-8
  Vary: Accept
  
  ""
  'NoneType' object has no attribute '__getitem__'
  </code></pre>
  
  <p>Same when i try to access <strong>/main/api/videos/?format=json</strong></p>
  
  <pre><code>TypeError("'NoneType' object has no attribute '__getitem__'",) is not JSON serializable
  </code></pre>
  
  <p>When i try to get the csv from <strong>/main/api/videos/?format=csv</strong>, It Tells me <strong>File Not Found</strong></p>
  
  <p>I might be missing some details, can't just let me know if you want more information.</p>
  
  <p>Thank you in advance</p>
  
  <p><strong>UPDATE</strong> 
  Now i just realize if i comment the function <strong>list</strong> the api's works fine..but my ajax is not working anymore because i don't have the 4 necessary things for to build the json</p>
  
  <pre><code>    result['data'] = serializer.data
      result['draw'] = ch['draw']
      result['recordsTotal'] = ch['total']
      result['recordsFiltered'] = ch['count']
  </code></pre>