From 890be10be749264b25ce293fc555fb51f2680836 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 8 Dec 2023 14:18:29 +0100 Subject: [PATCH] Change lookup field to id and add get_one by slug --- competition/views.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/competition/views.py b/competition/views.py index b2fffe1d..fd88360f 100644 --- a/competition/views.py +++ b/competition/views.py @@ -12,6 +12,7 @@ from rest_framework.decorators import action from rest_framework.exceptions import ValidationError from rest_framework.permissions import IsAdminUser, IsAuthenticated +from rest_framework.request import Request from rest_framework.response import Response from base.utils import mime_type @@ -107,9 +108,15 @@ class CompetitionViewSet(mixins.RetrieveModelMixin, """Naše aktivity""" queryset = Competition.objects.all() serializer_class = CompetitionSerializer - lookup_field = 'slug' permission_classes = (CompetitionRestrictedPermission,) + @action(detail=False, url_path=r'slug/(?P\w+)') + def slug(self, request: Request, slug: str = None) -> Response: + competition: Competition = self.get_queryset().get(slug=slug) + return Response( + CompetitionSerializer(competition, many=False).data + ) + class CommentViewSet( mixins.RetrieveModelMixin,