0

The basic DRF setup for my little Podcast Backend is working quite well:

router.register(r'episodes', views.EpisodeViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^api/', include(router.urls)),
]

in urls.py

and

class EpisodeSerializer(serializers.ModelSerializer):
    # show = ShowSerializer()

    class Meta:
        model = Episode
        depth = 1


class EpisodeDetailSerializer(serializers.ModelSerializer):
    chapters = ChapterMarkSerializer(source='chaptermark_set', many=True)
    media = MediaClipSerializer(source='mediaclip_set', many=True)
    show = ShowSerializer()

    class Meta:
        model = Episode
        depth = 1

in serializers.py as well as

class EpisodeViewSet(viewsets.ModelViewSet):
    queryset = Episode.objects.all().order_by('-published_at')

    def get_serializer_class(self):
        if self.action == 'retrieve':
            return EpisodeDetailSerializer
        return EpisodeSerializer

work well for creating a full episode list (I chopped down the classes a bit, they do additionally contain some field filters but this is not related) and also for a hyperlinked Detail view for each episode.

Additionally to that I need the possibility to query episodes by other fields. Especially a "number" field and the "show_id"

I can't seem to get behind how this is done in DRF. I already tried adding def get_queryset() to the EpisodeDetailSerializer Class but that didn't work.

So what I'm looking for is to process something like

/api/episodes/?show_id=2&number=24 

to deliver episode details instead of the default

/api/episodes/123

any help is appreciated.

1 Answer 1

1

You can use django_filters:

from django_filters.rest_framework import DjangoFilterBackend

class EpisodeViewSet(viewsets.ModelViewSet):
    queryset = Episode.objects.all().order_by('-published_at')
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('show_id', 'number')

    def get_serializer_class(self):
        if self.action == 'retrieve' or self.request.GET.get('show_id') and self.request.GET.get('number'):
            return EpisodeDetailSerializer
        return EpisodeSerializer
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this basically works already. There's just one problem: This only affects the list view of all episodes. The EpisodeDetailSerializer comes in place through that 'retrieve' action - at least that's what I think, I'm really just digging into this slowly. And what I would need is to get the detail Serializer returned for the episode filtered by show_id and number.
@Helmi you can try to check in get_serializer_class self.request.GET.get('show_id') and self.request.GET.get('number') also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.