question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search and filter doing nothing in custom ViewSet

See original GitHub issue

Hi,

I have a django rest application with multiple viewsets that are working correctly with datatables and this package. However, I have recently made a custom viewset which overrides the list() method of the viewset. Therefore, the ?format=datatables is not working correctly (the list function is overridden and only that functionality happens). Sorting, paging, and filtering are not working correctly. How can I also get the datatables formatted endpoint on server side processing to correctly paginate, filter, and search even though I have overridden the list() method of my viewset.

I tried the following which is pretty much the same as the docs:

Are my annotations breaking things?

class ValidationSummaryViewSet(viewsets.ViewSet):
    queryset = TargetConfirmation.objects.values('target_prediction__id').annotate(
        sample_id=F('target_prediction__id'), num_selected=Count('selected'), num_validated=Count('validated'),
        num_has_standard=Count('has_standard'))
    serializer_class = ValidationSummarySerializer

    def list(self, request):
        serializer = self.serializer_class(self.queryset, many=True)
        return Response(serializer.data)

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
neighlydcommented, Mar 5, 2019

Hey @Rubyj ,

From what I can see here, you aren’t running your serializer through either a paginator or a filter when you override list().

If you look at the code for list(), you’ll see that it runs the queryset through both self.filter_queryset() and self.paginate_queryset(). The first method checks your queryset filter backend and iterates through it, applying the filters that you have listed. The second method does something similar, in that it checks your object for a paginator and runs its pagination method.

However, since your list() method doesn’t call either of these methods, the resulting serializer has no way of being either filtered or paginated, whether through DRF Datatables or any other filtering and pagination package.

I would recommend looking at the source-code for the list() method mixin to refactor your own implementation so that you can implement these features.

2reactions
neighlydcommented, Mar 5, 2019

Hopefully it works!

If you aren’t familiar, there are two really fantastic websites that break down Django’s views and Django Rest Framework’s views and serializers into their component methods and mixins. They are really amazing resources. I highly recommend bookmarking them and having a look whenever you get stuck or want to modify existing behaviors.

Here is the Django Class-Based Views Website Here is the DRF Class Website

PS: I’m @neighlyd , not @izimobil. Not that it matters too much, so long as your issue gets resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Rest Framework ViewSet doesn't filter by field
When I use a generic search, like localhost:8000:/es/countries/?search=MX , it works fine. This is my ViewSet : class CountryViewSet(viewsets.
Read more >
The Ultimate Tutorial For Django Rest Framework: Filtering ...
Read our guide to filtering in the Django REST Framework. ... This time, let's think about how we can filter and search our...
Read more >
Generic views - Django REST framework
filter_backends - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the...
Read more >
10 things you need to know to effectively use Django Rest ...
Every time you write views that should do more than one thing, a viewset is ... You can even add some custom action...
Read more >
How to create search and filted fields in custom @action ...
Hello, now i'm studying DRF and have to do project with photo albums. One of my tasks is to create custom action "patch"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found