2👍
✅
You could try to inherit from DefaultRouter which is responsible for api root view and redefine get_api_root_view method.
class MyRouter(routers.DefaultRouter):
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, *args, **kwargs):
ret = OrderedDict()
namespace = request.resolver_match.namespace
for key, url_name in api_root_dict.items():
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:
# Don't bail out if eg. no list routes exist, only detail routes.
continue
ret['book-featured-list'] = '%s%s' % (ret['books'], 'featured/')
return Response(ret)
return APIRoot.as_view()
P.S. sorry, didn’t see your comment before I posted the answer
2👍
You can install package django-rest-swagger, just follow here: https://github.com/marcgibbons/django-rest-swagger
It is more powerful than DRF’s api list page. It will list all the rest apis (include list_route/detail_route apis) for your modules, and you can also do some api test (CRUD) on the page.
- Django – how to determine if model class is abstract
- How do i loop through fields of an object?
- How to copy file from one path to other using django-storages and amazon S3?
- SSH into Django Shell
0👍
I use this solution based on likeon’s answer:
class MyRouter(routers.DefaultRouter):
def get_api_root_view(self, api_urls=None):
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)
api_root_dict['books/featured'] = 'book-featured'
return self.APIRootView.as_view(api_root_dict=api_root_dict)
- Serving static files with Nginx + Gunicorn + Django
- How to create groups and assign permission during project setup in django?
- Why does Django do cascading deletes on foreign keys?
- Django form – django-bootstrap-datepicker-plus is not rendering datepicker
- When posting to my Django API : errorcom.google.gson.JsonParseException: unable to parse json
-1👍
Can you try with, put this below your urlpatterns.
Your url will be like this:
http://localhost:8000/api/books/
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = format_suffix_patterns(urlpatterns)
- Dynamic one-line output in Django management command
- Create Read-Only MySQL Database Connection in Django
- Got AttributeError when attempting to get a value for field on serializer
Source:stackexchange.com