How to add namespace url to a django-rest-framewor

2019-04-05 06:54发布

问题:

I would like to add a url namespace to my api router but when I do the router still looks for urls without a namespace:

router = DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'events', EventViewSet)
router.register(r'comments', CommentViewSet)

urlpatterns = patterns('apiroot.views',
                       url(r'^', include(router.urls, namespace='api')),
                       )

The browsable api looks for url names like 'user-list' and 'user-detail' still instead of 'api:user-list' which is what I would like to have happen.

I'm assuming there is an easy fix for this (it seems like a pretty standard thing to want to do) but I can't find any solution in the django-rest-framework docs. If I'm missing something from the docs, please share a link or if I am approaching this wrong (i.e "why would you do that, it's bad practice" or whatever) please explain why or what the correct way to manage api urls would be. Thanks!

回答1:

There's a pull request open for this currently. Please feel free to join the discussion.



回答2:

This is a very old question and the currently accepted answer from Carlton Gibson is just a link to a pull request on GitHub.

When searching the web for using Django REST Framework routers with namespace, it comes first in the results. It seems to be the only question at SO about this topic.
Since there is no concrete answer with code example, here I'd like to add my solution.

Let's take the EventViewSet from the OP's answer. For this viewset there should be a model class Event and a model serializer EventSerializer.
The EventSerializer could look like this:

class EventSerializer(serializers.HyperlinkedModelSerializer):
    # any other fields
    class Meta:
        model = Event
        # anything else
        extra_kwargs = {
            'url': {'view_name': 'api:event-detail'}
        }

That is an example for HyperlinkedModelSerializer which includes url field.

Any hyperlinked fields like HyperlinkedIdentityField or HyperlinkedRelatedField, which take view_name as an argument should be passed the correct view name containing the namespace, either in the declaration or through the extra_kwargs.
For an assumed CommentSerializer (corresponding to the CommentViewSet) it could look like:

class EventSerializer(serializers.HyperlinkedModelSerializer):
    comments = serializers.HyperlinkedRelatedField(
        many=True,
        read_only=True,
        view_name='api:comment-detail'
    )