0
votes

I have a URL (urls.py)

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from rds import views

urlpatterns = patterns('',
    url(r'^markit/cdxcomposites/$', views.File_List.as_view()),
    url(r'^markit/cdxcomposites/(?P<pk>[0-9]+)/$', views.File_Detail.as_view()), 'cdxcomposites_date'),
)

urlpatterns = format_suffix_patterns(urlpatterns, allowed=['csv', 'json', 'raw', 'xml', 'yaml'])

And I'm trying to use the following view (views.py) -

class File_List(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    queryset = cdx_composites_csv.objects.using('markit').all()
    serializer_class = CDX_compositesSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

to be able to filter by the model field 'Date'

http://localhost:8080/markit/cdxcomposites/?Date=2014-06-26&format=xml

It never seems to filter always returning all results.

I've tried all the samples provided here -

http://www.django-rest-framework.org/api-guide/filtering#overriding-the-initial-queryset

But nothing seems to be taking. I've tried scrapping the mixins and doing a basic class based view and still no dice.

I know I'm missing something obvious but I'm unsure what.

For example doing the most simple of views -

class File_List(generics.ListAPIView):
    serializer_class = CDX_compositesSerializer

    def get_queryset(self):
        """
        This view should return a list of all the purchases for
        the user as determined by the username portion of the URL.
        """
        Date = self.kwargs['Date']
        return cdx_composites_csv.objects.using('markit').filter(Date__Date=Date)

results in the error -

KeyError at /markit/cdxcomposites/

'Date'

2

2 Answers

1
votes

Usually the GET parameters are passed in a dictionary attached to the request object. See the documentation on request objects.

Try:

Date = self.request.GET.get('Date')
0
votes

'Date' in your case is not a keyword argument but a query parameter. I would do that like this:

# first import the needed modules, classes, functions, ...
import re
from django.http import Http404


class File_List(generics.ListAPIView):
    serializer_class = CDX_compositesSerializer

    def get_queryset(self):
        queryset = cdx_composites_csv.objects.using('markit').all()
        # get the query parameter or None in case it is empty
        date = self.request.QUERY_PARAMS.get('Date', None)
        # regex for correct date
        regex_date = re.compile(r'^\d{4}-\d{2}-\d{2}$')
        # filter your queryset if you really got a correct date
        if date is not None and regex_date.match(date):
            queryset = queryset.filter(Date__contains=date)
        # if there are no results return 404 NOT FOUND
        if not queryset:
            raise Http404
        return queryset

Perhaps there are better solutions, but this should work. Remember always to check the user input.

I don't know your model, but maybe you could implement a manager that would return all objects using 'markit'. Than you could get the queryset like this (just a possible example):

queryset = cdx_composites_csv.markit.all()

Here 'markit' is an attribute in your model holding your custom manager:

MarkitManager(models.Manager):
    def get_query_set(self):
        return Super(MarkitManager, self).get_query_set().using('markit')

More about filtering against query parameters can be found in the official documentation of DRF: http://www.django-rest-framework.org/api-guide/filtering#filtering-against-query-parameters