[Answered ]-Django rest framework and multiple routers

2👍

Allright, i figured it out. The SimpleRouter class in the Django Rest Framework defines the routes in the class, and doesn’t bind the values in the __init__ phase. SimpleRouter.routes is a list.

Since lists are mutable, all instances of SimpleRouter point at the same route-list when creating the SimpleRouter instances. When i appended a Route to admin_router.routes, it also updated user_router.routes….

The way to overcome this is to define separate Router classes which override the router-list.

class AdminRouter(SimpleRouter):
    routes = # [ ... my list of Route()'s ... ]


class UserRouter(SimpleRouter):
    routes = # [ ... my other list of Route()'s ...]


admin_router = AdminRouter()
user_router = UserRouter()

Leave a comment