DRF更改默认视图集的lookup_field以进行自定义操作

时间:2019-06-06 12:38:05

标签: python django django-rest-framework

如何为DRF Viewset中的自定义操作更改默认的查找参数? 这是我的Viewset(简体)

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)

我希望用户输入/invitations/accept-invitation/abccba之类的网址,其中abccba是随机令牌字符串。 key-是邀请模型中的唯一字段。我知道我可以为每个Viewset设置lookup_field='key',但是我希望所有其他操作仍使用默认的lookup_field='pk'。如何实现我想要的?

1 个答案:

答案 0 :(得分:0)

您可以重写get_object()方法来实现。这是GenericAPIView代码:

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

然后添加验证以检查当前正在触发哪个操作,并相应地修改lookup_field / lookup_url_kwarg。您在self.action函数中的get_object应该等于accept_invitation(您要修饰的函数的名称)。

这里的问题是,您正在使用带有detail=False的动作装饰器,因此查找字段没有任何意义。我的方法适用于其中一条评论中提到的常规详细路线:<your_api_url>/<invitation-viewset-path>/<key>/accept-invitation/

相关问题