Nov-07-2017, 11:36 AM
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):
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
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
