python - DRF 更改默认视图集的 lookup_field 以进行自定义操作
问题描述
如何更改 DRF 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)
我想当用户输入 url 时/invitations/accept-invitation/abccba
,abccba
随机令牌字符串在哪里。key
- 是邀请模型中的一个独特领域。我知道我可以设置 per-Viewset lookup_field='key'
,但我希望所有其他操作仍使用 default lookup_field='pk'
。我怎样才能达到我想要的?
解决方案
您可以覆盖该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/
.