chibisov / drf-extensions

DRF-extensions is a collection of custom extensions for Django REST Framework
http://chibisov.github.io/drf-extensions/docs
MIT License
1.47k stars 208 forks source link

the response of the route api-root is '{}' #152

Open ghost opened 8 years ago

ghost commented 8 years ago

the response of the route api-root is '{}'

ghost commented 8 years ago

def get_api_root_view(self): """ Return a view to use as the API root. """ api_root_dict = OrderedDict() list_name = self.routes[0].name for prefix, viewset, basename in self.registry: api_root_dict[prefix] = list_name.format(basename=basename)

    class APIRoot(views.APIView):
        _ignore_model_permissions = True

        def get(self, request, format=None, *args, **kwargs):
            ret = OrderedDict()
            namespace = request.resolver_match.namespace
            for key, url_name in api_root_dict.items():

                import re
                match = re.search(r'parent_lookup_\w+', key)
                if match:
                    k = match.group()
                    kwargs[k] = k.split("parent_lookup_")[-1]
                    kwargs[k] = 1

                if namespace:
                    url_name = namespace + ':' + url_name
                try:
                    ret[key] = reverse(
                        url_name,
                        args=args,
                        kwargs=kwargs,
                        request=request,
                        format=kwargs.get('format', None)
                    )
                except NoReverseMatch:
                    pass
            return Response(ret)

    return APIRoot.as_view()