DRF only API shows only one router

Essentially, depending on the order in which I add my routes to mine urlpatterns

, the viewable API will only show one router at a time. Here's my code:

urls.py:

from django.conf.urls import url, include
from rest_framework import routers

from .views import PlantViewSet

# url router
router = routers.DefaultRouter()
router.register(r'plants', PlantViewSet, base_name='Plants')

djoser_urls = [url(r'^', include('djoser.urls')), ]

urlpatterns = [
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^docs/', include('rest_framework_swagger.urls')),
    # url(r'^', include(router.urls)),
    # url(r'^', include('djoser.urls')),
] + djoser_urls + router.urls

      

This only displays tags djoser

:

djoser_only

However, by simply changing the order in which I add the URL:

urls.py:

from django.conf.urls import url, include
from rest_framework import routers

from .views import PlantViewSet

# url router
router = routers.DefaultRouter()
router.register(r'plants', PlantViewSet, base_name='Plants')

djoser_urls = [url(r'^', include('djoser.urls')), ]

urlpatterns = [
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^docs/', include('rest_framework_swagger.urls')),
    # url(r'^', include(router.urls)),
    # url(r'^', include('djoser.urls')),
] + router.urls + djoser_urls

      

This only displays tags router

!

router_only

The same thing happens when I just use the lines include()

I have commented, whichever comes first in the list, this is the only router that is shown. In addition, no matter which router receives, URL-addresses api-auth/

and docs/

will never be displayed. Is there a way to get a single root api without creating your own custom view?

+3


source to share


1 answer


This has nothing to do with the Django REST framework, it is due to the way Django deals with duplicate URLs.

You are trying to parse the same url with two different views: the DRF router index and the djoser root view. Django will only render the first view that matches the search pattern it finds, which are usually the first URLs included in url patterns.

The Django REST framework will also not detect multiple available routers and group them on the same page, which sounds like what you're hoping to see. Even if it were possible, djoser is not using a router , so DRF really couldn't enable it.



Is there anyway to get a single api root without having to create your own custom view?

So, to answer the main question, no, that Django REST Framework cannot automatically group these views together. You will need to create your own client view to handle this.

+3


source







All Articles