#Cart Viewset Api doesnt appear in API Root but others do?

1 messages · Page 1 of 1 (latest)

queen tide
#

``router = DefaultRouter()
router.register('cart', CartViewSet, basename='cart'),

urlpatterns = [
path('', include(router.urls)),
path('cart/uuid:cart_id/items/', CartItemViewSet.as_view({'get': 'list', 'post': 'create'}), name='cart-items'),
path(
'cart/uuid:cart_id/items/uuid:item_id/',
CartItemViewSet.as_view({'patch': 'partial_update', 'delete': 'destroy'}),
name='cart-item',
),
]``

In the code above, the api for CartViewSet doesnt appear in the API root 127.0.0.1:8000/api/ while the api for the other two viewsets do so
``router = routers.DefaultRouter()

router.register(r"category", views.CategoryViewSet, basename="category"),
router.register(r"product", views.ProductViewSet, basename="product"),
urlpatterns = router.urls``

naive trout
#

Yes, this is due to the router creating the API root view for you within the router.

queen tide
naive trout
#

oh one minute, I mis read. Could you share all of your urls.py (project one and the ones where you are registering these routes)

queen tide
#

main project's urls.py
urlpatterns = [ path('admin/', admin.site.urls), path('api/', include('src.product.urls')), path('api/', include('src.basket.urls')), # JWT Authentication path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), # API Documentation path('api/schema/', SpectacularAPIView.as_view(), name='schema'), path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

#

cart's urls.py
``router = DefaultRouter()
router.register('cart', CartViewSet, basename='cart')

urlpatterns = [
path('', include(router.urls)),
path('cart/uuid:cart_id/items/', CartItemViewSet.as_view({'get': 'list', 'post': 'create'}), name='cart-items'),
path(
'cart/uuid:cart_id/items/uuid:item_id/',
CartItemViewSet.as_view({'patch': 'partial_update', 'delete': 'destroy'}),
name='cart-item',
),
]``

#

product's urls.py
``router = routers.DefaultRouter()

router.register(r"category", views.CategoryViewSet, basename="category"),
router.register(r"product", views.ProductViewSet, basename="product"),

urlpatterns = router.urls
``

naive trout
#

So you have created 2 routers, which is why you are only seeing one set of Viewsets in the API root. Where are are the cart URLs included in the the main project? Is it this line?

 path('api/', include('src.basket.urls')),
naive trout
#

Generally you want a single router, I typically define this in the project urls.py and register my Viewsets there. Or have a separate api urls.py which deals with this

queen tide
queen tide
#

@naive trout a friend of mine had been using rest_framework_nested , would you recommend with or against it? I will have to install the package for it

naive trout
#

I don't think I have used it myself so couldn't comment. If it fit's your need then give it a try?

queen tide