Register a route for two different viewsets that inheruse the same model

Viewed 1344

I'm using Django REST framework and I've a configuration like this one:

router.register(r'foo', FooViewSet)
router.register(r'foo-mini', MinFooViewSet)

FooViewSet and MinFooViewSet are pretty identical ("min" hones inherit from the former), use same queryset but simply change the serializer_class.

The serializer class used by MinFooViewSet returns less fields from the same model.

I've a weird behavior (probably standard but I don't understand it): seems that I can have only one route for every model. In facts the generated API index is like the following:

"foo": "http://localhost:8001/api/v1/foo-mini",
"foo-mini": "http://localhost:8001/api/v1/foo-mini",

So both endpoints point to the same URL, that use the last (MinFooViewSet) configuration.

What I'm missing?

2 Answers

Use base_name='something'

router.register(r'foo', FooViewSet,base_name='foo')
router.register(r'foo-mini', MinFooViewSet,base_name='foo_mini')
Related