Python Forum

Full Version: django rest framework - custom routes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

Firstly, I hope that this query is in the correct format, apologies if it breaks any forum guidelines - let me know and I'll amend it.

Some background - I have been asked by a client to build a system in python 3 + django 1.11. I have no previous experience of either (they are aware of this), having done most of my web development work for the last 15 years in either PHP with or without a framework (eg Zend, Laravel, Yii) or prior to that, ASP.NET and prior even to that, using ColdFusion. So I suppose I come with some baggage in terms of pre-conceptions of how django should work which is hampering me.

The system is really just a set of models and some APIs over the top of them. Mostly CRUD, but I also need some custom APIs too. After several failed attempts, I have managed to get the models and CRUD APIs working using the djangorestframework package. I am using the default router from the rest framework in the file and Viewsets to manage file.

Now I am stuck. I cannot figure out how to add additional routes - everything I try leads to errors.

As an example. Say I have a Project model. A Project has zero to many Task models associated with it. Currently, I would have something like this in the routing (I have removed some lines):

from django.conf import settings
from rest_framework import routers
from django.conf.urls import include, url
from django.contrib import admin
import projects.api.views as api_views

router = routers.DefaultRouter()
router.register(r'project', api_views.ProjectAPIViewset)
router.register(r'task', api_views.TaskAPIViewset)

urlpatterns = [
    url(r'^api/', include(router.urls)),
What I would like is to add a route along these lines:
GET /api/project/12345/tasks

This would would ideally run a 'tasks' method inside the ProjectAPIViewset class which would return a list of all tasks that have a project_id of 12345. Unless that is not the django way?

I'd be very grateful if somebody could explain the steps I need to take to achieve this? I have many non-CRUD APIs that I need to add and this has now stumped me for 2 days.

Many thanks in advance Smile
Note: I have tried using the @list_route and detail_route decorators on the methods I want called via custom routes, but I don't appear to be getting those extra routes automatically. These currently give 404s
I think you better like this :

project/ (project configuration)

from django.urls import path, include

urlpatterns = [
   path("api/", include('app.urls')),

app/ (app is the app)

from django.urls import path
from app.views import tasks
urlpatterns = [
    path("project/<int:task_id>/tasks/", tasks.detail, name="task_detail"),


from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response

def detail(request):
    """ code heare. """
    data = {}
    return Response(data, status=status.HTTP_200_OK)
try url : http://localhost:8000/api/12345/tasks/