Using Django Rest Framework, I am creating endpoints for doctor model in my app app_api using ModelViewSet. I am encountering the following error on adding an extra action for updating the is_verified field with verify method having @action decorator.
error at /app_api/viewset/doctor/6/ redefinition of group name 'pk' as group 2; was group 1 at position 38
Please find the code below.
urls.py
router = DefaultRouter()
router.register('doctor', views.DoctorViewSet, basename='doctor')
urlpatterns = [
path('viewset/', include(router.urls)),
path('viewset/<int:pk>', include(router.urls)),
]
model.py
# Doctor Model
class Doctor(models.Model):
name = models.CharField(max_length=100)
designation = models.CharField(max_length=100)
description = models.TextField(blank=True, default='')
is_verified = models.BooleanField(default=False, blank=True)
serializer.py
# Doctor Model Serializer
class DoctorModelSerializer(serializers.ModelSerializer):
class Meta:
model = Doctor
fields = '__all__'
read_only_fields = ('is_verified',)
# Doctor Partial Update Model Serializer
class DoctorUpdateVerifyModelSerializer(serializers.ModelSerializer):
class Meta:
model = Doctor
fields = ('id', 'name', 'is_verified')
read_only_fields = ('name',)
views.py
class DoctorViewSet(viewsets.ModelViewSet):
serializer_class = DoctorModelSerializer
queryset = Doctor.objects.all()
def get_serializer_class(self):
serializer_class = self.serializer_class
if self.request.method == 'PATCH' and self.action == 'verify':
serializer_class = DoctorUpdateVerifyModelSerializer
return serializer_class
@action(methods=['patch'], detail=True)
def verify(self, request, pk=None):
serializer = self.get_serializer(self.get_object(), data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Response(serializer.data)
Now when I am adding an extra action for routing the method verify with the @action decorator, I am getting the following error on accessing url for /app_api/viewset/doctor/6/ and /app_api/viewset/doctor/6/verify/ where 6 is the primary key of doctor
error at /app_api/viewset/doctor/6/ redefinition of group name 'pk' as group 2; was group 1 at position 38
If I remove the detail=True from the verify method annotated with @action, it works well for /app_api/viewset/doctor/6/. My question is why this error is generated if I have @action decorator with detail=True.

