From 374a427a2934446d9382ae84889f66e2d32ae946 Mon Sep 17 00:00:00 2001 From: "cristian.popovici" Date: Mon, 12 Feb 2024 12:49:25 +0200 Subject: [PATCH 1/3] fix: BaseViewSet documentation --- drf_util/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drf_util/views.py b/drf_util/views.py index e27c967..ffba63f 100644 --- a/drf_util/views.py +++ b/drf_util/views.py @@ -114,7 +114,11 @@ def get_queryset(self) -> QuerySet: return queryset def get_serializer_by_action(self): - return self.serializer_by_action.get(self.action) + default_actions = ('list', 'retrieve', 'create') + + default_serializers = {action: getattr(self, f'serializer_{action}_class', None) for action in default_actions} + + return default_serializers.get(self.action) or self.serializer_by_action.get(self.action) def get_serializer_class(self): return self.get_serializer_by_action() or super().get_serializer_class() From b7e6af0811fee08234a6dfecf0b8a932065f5454 Mon Sep 17 00:00:00 2001 From: "cristian.popovici" Date: Mon, 12 Feb 2024 13:55:11 +0200 Subject: [PATCH 2/3] feat: optimize code --- drf_util/views.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/drf_util/views.py b/drf_util/views.py index ffba63f..31a4d21 100644 --- a/drf_util/views.py +++ b/drf_util/views.py @@ -114,11 +114,7 @@ def get_queryset(self) -> QuerySet: return queryset def get_serializer_by_action(self): - default_actions = ('list', 'retrieve', 'create') - - default_serializers = {action: getattr(self, f'serializer_{action}_class', None) for action in default_actions} - - return default_serializers.get(self.action) or self.serializer_by_action.get(self.action) + return getattr(self, f'serializer_{self.action}_class') or self.serializer_by_action.get(self.action) def get_serializer_class(self): return self.get_serializer_by_action() or super().get_serializer_class() From d8edafb9bab1e45f7ec59b298940f467be7176e0 Mon Sep 17 00:00:00 2001 From: "cristian.popovici" Date: Wed, 14 Feb 2024 15:40:14 +0200 Subject: [PATCH 3/3] - fixes after review --- drf_util/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drf_util/views.py b/drf_util/views.py index 31a4d21..8fd1107 100644 --- a/drf_util/views.py +++ b/drf_util/views.py @@ -114,7 +114,8 @@ def get_queryset(self) -> QuerySet: return queryset def get_serializer_by_action(self): - return getattr(self, f'serializer_{self.action}_class') or self.serializer_by_action.get(self.action) + return self.serializer_by_action.get(self.action) or getattr(self, f'serializer_{self.action}_class') \ + if self.action in ['list', 'retrieve'] else None def get_serializer_class(self): return self.get_serializer_by_action() or super().get_serializer_class()