Search code examples
djangodjango-rest-frameworkdjango-filters

Applying a Django FilterSet to an Annotated QuerySet?


I'm trying to apply a filter to an annotated queryset, like is done here: https://docs.djangoproject.com/en/2.0/topics/db/aggregation/#filtering-on-annotations

Similar to the snippet below, I'd like to make that highly_rated piece a FilterSet that picks up the request parameters

highly_rated = Count('books', filter=Q(books__rating__gte=7))
Author.objects.annotate(num_books=Count('books'), highly_rated_books=highly_rated)

I'd like to make that highly_rated piece a FilterSet that picks up the request queries. Something like:

class MainFilter(FilterSet):
    class Meta:
        model = Author
        fields = {
            author: ['in', 'exact'],
            genre: ['in', 'exact']
        }

class AnnotatedFilter(FilterSet):
    class Meta:
        fields = {
            'books__rating': ['gt', 'lt' ],
        }

class MyView(ListApiView):
    filter_class = AuthorFilter

    def get_queryset(self):
        annotated_filter = AnnotatedFilter(request) # how do I set up another filter based on the request?
        Author.objects.annotate(num_books=Count('books'), FilterSet=annotated_filter) # how do I apply it to an annotated queryset?

All so I can query something like:

api/authors/?genre=Fantasy&books__rating__gt=5

Where one of the parameters executes against all the records and other params execute against the annotated part.

Any help is greatly appreciated. Thanks!


Solution

  • Change get_queryset() method to

    def get_queryset(self):
        return Author.objects.annotate(num_books=Count('books')
    


    and define that field in Filter class as

    class AuthorFilter(FilterSet):
        num_books = filters.NumberFilter(name='num_books')
        class Meta:
            model = Author
            fields = ['num_books',]#other fields
    

    then use the api as /api/end/point/authorlist/?num_books=10

    EXAMPLE

    models.py

    from django.db import models
    
    
    class Sample(models.Model):
        foo = models.IntegerField()
        bar = models.IntegerField()
    

    views.py

    from django.db.models import F
    from django_filters import rest_framework as filters
    
    
    class MySampleAPI(viewsets.ModelViewSet):
        filter_backends = (filters.DjangoFilterBackend,)
        filter_class = SampleFilterClass
        serializer_class = SampleSerializerClass
    
        def get_queryset(self):
            return Sample.objects.annotate(foo_bar_sum=F('foo') + F('bar'))
    

    filters.py

    from django_filters import rest_framework as filters
    
    
    class SampleFilterClass(filters.FilterSet):
        sum = filters.NumberFilter(name='foo_bar_sum', lookup_expr='gte')
    
        class Meta:
            model = Sample
            fields = ['sum', ]