Ability to customize router URLs for custom actions, using url_path. by tanwanirahul · Pull Request #2010 · encode/django-rest-framework (original) (raw)

Not really, which is why I proposed in #1892 to just add dasherize_routes to Router init kwargs.

In which case I'd prefer to see a underscore-routers package or similar that includes alternate SimpleRouter and DefaultRouter classes, that use underscored styles. Eg.

class UnderscoreSimpleRouter(SimpleRouter):
    routes = [
        # List route.
         Route(
             url=r'^{prefix}{trailing_slash}$',
             mapping={
                 'get': 'list',
                 'post': 'create'
             },
             name='{basename}_list',
             initkwargs={'suffix': 'List'}
         ),
         # Dynamically generated list routes.
         # Generated using @list_route decorator
         # on methods of the viewset.
         DynamicListRoute(
             url=r'^{prefix}/{methodname}{trailing_slash}$',
             name='{basename}_{methodname}',
             initkwargs={}
         ),
         # Detail route.
         Route(
             url=r'^{prefix}/{lookup}{trailing_slash}$',
             mapping={
                 'get': 'retrieve',
                 'put': 'update',
                 'patch': 'partial_update',
                 'delete': 'destroy'
             },
             name='{basename}_detail',
             initkwargs={'suffix': 'Instance'}
         ),
         # Dynamically generated detail routes.
         # Generated using @detail_route decorator on methods of the viewset.
         DynamicDetailRoute(
             url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$',
             name='{basename}_{methodname}',
             initkwargs={}
         ),
     ]

Notice {methodnamehyphen} has become {methodname} throughout, and - has become _ in all the name parameters.

It's not terribly elegant, but it only needs to be done once.

I'd be against any further complexity in the router classes as they're already the least nice bit of API in rest framework so I've no problem with them continuing to only provide for a fairly restricted set of functionality.