Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF change default viewset's lookup_field for custom action

How can I change a default lookup parameter for my custom action in DRF Viewset? Here is my Viewset (simplified)

class InvitationViewSet(MultiSerializerViewSet):   
    queryset = Invitation.objects.all()  

    @action(
        detail=False,
        #url_path='accept-invitation/<str:key>/',
        #lookup_field='key'
    )
    def accept_invitation(self, request, key=None):
        invitation = self.get_object()
        with legal_invitation(invitation):
            serializer = self.get_serializer(invitation)
            invitation.accepted = True
            invitation.save()
        return Response(serializer.data)

I want when user enters url like /invitations/accept-invitation/abccba, where abccba is a random token string. key - is a unique field in Invitation model. I know I can set per-Viewset lookup_field='key', but I want all other actions still use default lookup_field='pk'. How can I achieve what I want?

like image 987
Chiefir Avatar asked Jun 06 '19 12:06

Chiefir


1 Answers

You can override the get_object() method to achieve that. This is the GenericAPIView code:

def get_object(self):
    """
    Returns the object the view is displaying.

    You may want to override this if you need to provide non-standard
    queryset lookups.  Eg if objects are referenced using multiple
    keyword arguments in the url conf.
    """
    queryset = self.filter_queryset(self.get_queryset())

    # Perform the lookup filtering.
    lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field

    assert lookup_url_kwarg in self.kwargs, (
        'Expected view %s to be called with a URL keyword argument '
        'named "%s". Fix your URL conf, or set the `.lookup_field` '
        'attribute on the view correctly.' %
        (self.__class__.__name__, lookup_url_kwarg)
    )

    filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
    obj = get_object_or_404(queryset, **filter_kwargs)

    # May raise a permission denied
    self.check_object_permissions(self.request, obj)

    return obj

Then add a verification to check which action is currently being triggered and modify the lookup_field/lookup_url_kwarg accordingly. Your self.action inside the get_object function should be equal to accept_invitation (the name of the function you are decorating).

The problem here though is that you're using an action decorator with detail=False, so the lookup field doesn't make a whole lot of sense. My approach works for a regular detail route as mentioned in one of the comments: <your_api_url>/<invitation-viewset-path>/<key>/accept-invitation/.

like image 83
henriquesalvaro Avatar answered Oct 09 '22 01:10

henriquesalvaro