My question boils down to one of best practice for REST and Views in Django. I am reworking the REST api for my current project, and that includes reworking our Views (Controller in traditional MVC) somewhat. Currently, everything is tunneled through POST requests, even things that should be a simple GET.
From a rest perspective, a list of all jobs should be returned with a URL like:
api/jobs/
and a single job (with ID 1) should be retrieved with:
api/jobs/1
and finally this should return all tasks that are part of job 1
api/jobs/1/tasks/
This is the philosophy I'm using for the new system, but I'm torn on the implementation. Should both of these get their own View in Django, or should one view handle both.
Currently I am using one View, where all relevant urls route to it, with logic like this;
def get(self, request, id=None, task_id=None):
if id is None:
return Job.objects.all() # This will be the LIST'
if task_id is not None:
return Job.objects.get(id=id).task_set.all()
return Job.objects.get(id=id)
This seems like it could lead to very complex and verbose views if more complexity is added. If I wanted to add similar logic to the task logic for some other resource, would I keep adding to the view, or should each of these get its own View?