Django REST框架 - 将额外参数传递给操作

Anu*_*TBE 12 django django-rest-framework

我正在使用Django 2.0Django REST Framework

我已经创建了一个从数据库中删除特定对象的操作方法

联系人/ views.py

class ContactViewSet(viewsets.ModelViewSet):
    serializer_class = ContactSerializer
    permission_classes = (IsAuthenticated, AdminAuthenticationPermission,)

    # others actions goes here

    @action(methods=['delete'], detail=False, url_path='delete_phone/<phone_pk>/')
    def delete_phone(self, request, pk=None):
        contact = self.get_object()
        print(contact)
        print(pk)
        print(self.kwargs['phone_pk'])
        return Response({'status': 'success'})
Run Code Online (Sandbox Code Playgroud)

应用程序/ urls.py

router.register(r'contacts', ContactViewSet, 'contacts')

api_urlpatterns = [
    path('', include(router.urls)),
]
Run Code Online (Sandbox Code Playgroud)

但当我访问

DELETE: http://url/api/contacts/delete_phone/1/
Run Code Online (Sandbox Code Playgroud)

它给出了page not found错误.

在错误页面中,列出了已尝试的网址模式

api/ ^contacts/delete_phone/<phone_pk>//$ [name='contacts-delete-phone']
api/ ^contacts/delete_phone/<phone_pk>\.(?P<format>[a-z0-9]+)/?$ [name='contacts-delete-phone']
Run Code Online (Sandbox Code Playgroud)

die*_*us9 18

如果您不愿意/不想要/无论什么安装drf-nested-routers,都可以通过执行以下操作来实现:

@action(detail=True,
        methods=['delete'],
        url_path='contacts/(?P<phone_pk>[^/.]+)')
def delete_phone(self, request, phone_pk, pk=None):
    contact = self.get_object()
    phone = get_object_or_404(contact.phone_qs, pk=phone_pk)
    phone.delete()
    return Response(.., status=status.HTTP_204_NO_CONTENT)
Run Code Online (Sandbox Code Playgroud)

诀窍是将正则表达式放入url_path装饰器的参数中,并将其传递给装饰器方法(避免使用just,pk否则它将与第一个pk冲突

经过测试:

Django==2.0.10
djangorestframework==3.9.0
Run Code Online (Sandbox Code Playgroud)


Anu*_*TBE 6

使用drf-nested-routers解决了问题

对于那些需要它的人,安装插件并配置 urls.py

from rest_framework_nested import routers

router = routers.SimpleRouter()
router.register(r'contacts', ContactViewSet, 'contacts')
contact_router = routers.NestedSimpleRouter(router, r'contacts', lookup='contact')
contact_router.register(r'phone_number', ContactPhoneNumberViewSet, base_name='contact-phone-numbers')

api_urlpatterns = [
    path('', include(router.urls)),
    path('', include(contact_router.urls))
]
Run Code Online (Sandbox Code Playgroud)