DRF nested endpoint without drf nested routers?

Viewed 77

It's possbile to having nested endpoint for the API without drf-nested-routers library?

I tried to install the library to my project but seems not success, I afraid the installation make the project crush so I stopped from install the package.

Now I'm write api to get data from models, the normal endpoint of it will be

/test?limit=10&offset=10

And I would like to design search function and view more detail of it by calling this api

/test/{data_id}

for example, calling first endpoint will get data:

{
  id:1,
  name:test,

  id:2,
  name:test2,

}

when I call second endpoint, it will show

/test/1
{
  id:1,
  name:test,
  age:17,
  gender:M,
}

It's possible to achieve without nested routers package? I tried but it will become lookup and can't display detail information

1 Answers

I find a way to achieve my target

add following re_path statement in the urls.py

router.register("test",TestViewSet)

urlpatterns =[

re_path('^test/(?P<test_uid>.+)/', TestIDViewSet.as_view({'get':'list'})),
]
urlpatterns += router.urls

and create new viewset in viewset.py

Related