Nested views and routes of Django rest

Can I set viewpoints and create routes that accept pkas URL parameters?

primarily

class TaskView(viewsets.ModelViewSet):
    model = Task

this works fine and maps to the url task/, so it task/1/provides task data with identifier 1. Now I want to create instancetasks with CRUD operations, just like for a task, so I would like to have

class InstanceView(viewsets.ModelViewSet):
        model = Instance

maps to task/{pk}/instance, where pkis the task identifier.

How can i do this? Is it possible? PS: I have seen that there is @actionand @link, but using them, I lose because of the presence of all that made frame.

+4
source share
2

: drf-nested-viewsets drf-nested-routers.

DRF , . README , . , DefaultRouters ( API).

DRF ( : ) ( HyperlinkedModelSerializer) . , URL- URL- . .

get_queryset . DRF Nested Viewsets url self.kwargs , , DRF, .

. , , get_queryset url.

+3

DRF .

+6

Source: https://habr.com/ru/post/1539303/


All Articles