Django rest api root view showing links of another app

Viewed 683

I have created a projects which has two apps in it, app1, app2 both having,

router = routers.DefaultRouter()

in its url.py, both of them as a standalone app ie if other app router is commented, will show correct default root API of all links. But when launching both of them, I am having an issue of apps2 api links coming in app1, by overwriting similar links of app1 which is similar in terms of serilizer, viewset and model nameAPI default view

This is my projects urls.py file

from django.urls import path, include

urlpatterns = [
    path('app1/', include('app1.urls'),name='oc_url'),
    path('app2/', include('app2.urls'),name='pc_url'),
    
]

App1 urls.py is

from app1.views import ActiveUsersViewSet,ProjectsViewSet,TestsExecutedViewSet,TestRunsViewSet,DefectsViewSet,FeaturesViewSet
router = routers.DefaultRouter()
router.register(r'activeusers', ActiveUsersViewSet)
router.register(r'projects', ProjectsViewSet)
router.register(r'testsexecuted', TestsExecutedViewSet)
router.register(r'testruns', TestRunsViewSet)
router.register(r'defects', DefectsViewSet)
router.register(r'features', FeaturesViewSet)

App2 urls.py

from app2.views import ActiveUsersViewSet,ProjectsViewSet,SessionsHistoryViewSet
router = routers.DefaultRouter()
router.register(r'activeusers', ActiveUsersViewSet)
router.register(r'projects', ProjectsViewSet)
router.register(r'sessionshistory', SessionsHistoryViewSet)

The issue is coming with api which is related to models which has same name in both apps. Is it because I have same models with same name in both apps? Any suggestions to fix this?

1 Answers

After some digging around, it looks like the issue is conflicting url_names. In your case, you will have conflicting url_names for your ActiveUsersViewsets, and ProjectsViewSets.

You can get around this by specifying a base_name. This will ensure that all of your url_names are unique:

app1/urls.py

from app1.views import ActiveUsersViewSet,ProjectsViewSet,TestsExecutedViewSet,TestRunsViewSet,DefectsViewSet,FeaturesViewSet
router = routers.DefaultRouter()
router.register(r'activeusers', ActiveUsersViewSet, "app1/activeusers")
router.register(r'projects', ProjectsViewSet, "app1/projects")
router.register(r'testsexecuted', TestsExecutedViewSet, "app1/testsexecuted")
router.register(r'testruns', TestRunsViewSet, "app1/testruns")
router.register(r'defects', DefectsViewSet, "app1/defects")
router.register(r'features', FeaturesViewSet, "app1/features")

app2/urls.py

from app2.views import ActiveUsersViewSet,ProjectsViewSet,SessionsHistoryViewSet
router = routers.DefaultRouter()
router.register(r'activeusers', ActiveUsersViewSet, "app2/activeusers")
router.register(r'projects', ProjectsViewSet, "app2/projects")
router.register(r'sessionshistory', SessionsHistoryViewSet, "app2/sessionhistory")
Related