[Python] Removing unused lookup_field generated from router on viewset

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have a viewset on a "/me" endpoint that doesn't require a lookup_field for detailing, as it uses the current request user for filtering, retrieving a single result.

    class ClientProfileDetail(
    mixins.RetrieveModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet
    ):
    permission_classes = [IsOwnerOrAdmin]
    queryset = ClientProfile.objects.all()
    serializer_class = ClientProfileSerializer

    def get_object(self):
    queryset = self.get_queryset()
    obj = get_object_or_404(queryset, pk=self.request.user)
    return obj


    I'm registering the viewset using the default router:

    router.register(r"me", ClientProfileDetail)


    But even though i have no use for the on the URL, the router register these URLs:

    profile/ ^me/(?P<pk>[^/.]+)/$ [name='clientprofile-detail']
    profile/ ^me/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='clientprofile-detail']


    My question is: How can i remove the (?P<pk>[/.]+)/$ on my routes

    Overriding get_object() as explained here: RetrieveAPIView without lookup field? didn't work

    Continue reading...

Compartilhe esta Página