Is it possible to do a multiple id request?

4,946 views
Skip to first unread message

Nestor Manrique

unread,
Mar 14, 2013, 6:14:12 PM3/14/13
to django-res...@googlegroups.com
Hi! I'm kind of new working with rest and I wonder how I can support a multiple id request like '/albums/1,2,3,4,5'. I'm working with backbone and backbone-relational and it would be nice if I could retrieve multiple models at once. I've read the docs but haven't seen it around.

Thanks in advance!

Nestor

Tom Christie

unread,
Mar 18, 2013, 8:08:39 AM3/18/13
to django-res...@googlegroups.com
Hi Nestor,

  Take a look through the filtering documentation...


There's various examples of how to filter querysets, hopefully that'll put you on the right track to getting this particular use-case working.  If there's anything that isn't sufficiently explained, please do follow up on this thread.

All the best,

  Tom

Scott White

unread,
Mar 18, 2013, 11:09:43 AM3/18/13
to django-res...@googlegroups.com
Hi Nestor,

I think something like the following view would work:

class SomeList(generics.ListAPIView):
    """
    API endpoint representing a list of somethings.
    """

    model = SomeModel
    serializer_class = SomeModelSerializer
    filter_fields = ('id',)

    def get_queryset(self):
        """
        Override get_queryset() to filter on multiple values for 'id'
        """

        id_value = self.request.QUERY_PARAMS.get('id', None)
        if id_value:
            id_list = id_value.split(',')
            queryset = queryset.filter(id__in=id_list)

        return queryset

I guess this will work even without 'id' in the filter_fields, but it's probably a good idea to include it. The URL would then be something like 'www.example.com/api/albums/?id=1,2,3,4,5'

Regards,
Scott

Scott White

unread,
Mar 18, 2013, 11:16:38 AM3/18/13
to django-res...@googlegroups.com
Oops, this:

queryset = queryset.filter(id__in=id_list)

should be this:

queryset = SomeModel.objects.filter(id__in=id_list)

Google groups, y u no let me edit post?

Nestor Manrique

unread,
Mar 18, 2013, 2:00:23 PM3/18/13
to django-res...@googlegroups.com
Thank you very much both of you, it worked perfectly!

Nestor

Miguel Fiandor Gutiérrez

unread,
Jun 6, 2014, 11:49:07 AM6/6/14
to django-res...@googlegroups.com
I am having big problems here to make it work. It is a very similar case, but my filter field is a foreign field.
I am following up Scott's example, and another approach defining a FilterSet, but none of them works. 
It returns an exception: EmptyResultSet, which is kind of funny because the queryset is returned perfectly.

--------------------------------
1st approach

class PublicAdministrationIndicatorPaginated(CacheResponseMixin, CustomReadOnlyModelPaginatedViewSet):
    """
    API endpoint that allows to: view, filter, search and order.
    Formats allowed: json, api, csv, xml
    """
    queryset = PublicAdministrationIndicator.objects.all()
    serializer_class = PublicAdministrationIndicatorSerializer
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES + [PaginatedCSVRenderer, XMLRenderer]
    filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter,)


    filter_fields = ('public_administration', 'public_administration__ine_code', 'public_administration__size', 'public_administration__type', 'public_administration__territory', 'indicator', 'indicator__year', 'has_value')
    search_fields = ('public_administration', 'indicator')
    ordering_fields = ('public_administration', 'indicator', 'absolute_cons')

    def get_queryset(self):
        """
        Override get_queryset() to filter on multiple values for 'PublicAdministrationIndicator_indicator__year'
        """
        queryset = PublicAdministrationIndicator.objects.all()

        years_value = self.request.QUERY_PARAMS.get('indicator__year', None)
        if years_value:
            year_list = years_value.split(',')
            print year_list
            queryset = PublicAdministrationIndicator.objects.filter(indicator__year__in=year_list)
            print queryset.count()  # actually prints a length of queryset of 525917

        return queryset


------------------------
2nd approach:

class PublicAdministrationIndicatorFilter(django_filters.FilterSet):
    indicator__year = django_filters.ModelMultipleChoiceFilter(
            name="indicator_id__year",
            queryset=PublicAdministrationIndicator.objects.all(),
            lookup_type='in'
    )

    class Meta:
        model = PublicAdministrationIndicator
        fields = ('indicator', 'indicator__year',)


class PublicAdministrationIndicatorPaginated(CacheResponseMixin, CustomReadOnlyModelPaginatedViewSet):
    """
    API endpoint that allows to: view, filter, search and order.
    Formats allowed: json, api, csv, xml
    """
    queryset = PublicAdministrationIndicator.objects.all()
    serializer_class = PublicAdministrationIndicatorSerializer
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES + [PaginatedCSVRenderer, XMLRenderer]
    filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter,)

    filter_class = PublicAdministrationIndicatorFilter
    search_fields = ('public_administration', 'indicator')
    ordering_fields = ('public_administration', 'indicator', 'absolute_cons')



Please, I need to know why is raising that error when the queryset seems to be well computed, and all results are on it.

Cheers,


Miguel

Miguel Fiandor Gutiérrez

unread,
Jun 6, 2014, 11:57:49 AM6/6/14
to django-res...@googlegroups.com
I hate when I spend hours with a problem and right after asking for help I find out what is wrong...

Anyway, I found out that in the first approach that I wrote, the one of Scott's code example, it was just a matter of removing the line: 

filter_fields = (.....) 

Since, seems to not get alone well with the queryset definition that comes after. Just removing it, it worked like a charm :-)
Reply all
Reply to author
Forward
0 new messages