Python Forum
django rest framework - custom routes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
django rest framework - custom routes
#1
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 urls.py file and Viewsets to manage views.py 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)),
    url(r'^admin/', admin.site.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
Reply
#2
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
Reply
#3
I think you better like this :

project/settings.py (project configuration)

from django.urls import path, include

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

app/urls.py (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"),
]


app/views/tasks.py

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

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Extend a serializer in Django Rest-Framework Dexty 0 1,689 Aug-10-2022, 02:53 PM
Last Post: Dexty
  Integrating vue.js with Django framework saisankalpj 0 1,149 Jun-29-2022, 01:32 PM
Last Post: saisankalpj
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,246 Jun-30-2019, 12:21 AM
Last Post: scidam
  How to access routes using Bottle Framework nikos 5 4,203 Feb-13-2019, 11:43 PM
Last Post: nikos
  Django Rest Framework URL issue marcus 0 2,480 Feb-07-2018, 10:31 PM
Last Post: marcus
  Django REST - input of some form fields is send twice to the backend ann 4 3,942 Jan-03-2018, 05:43 PM
Last Post: ann
  Django allauth saving custom user profile fields with signup form prithvi 1 10,462 Aug-08-2017, 03:51 PM
Last Post: prithvi
  running Django app with REST chemseddineZ 2 3,248 Apr-07-2017, 03:18 PM
Last Post: shahpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020