1

It seems to me that there should be an automatic way to query the results of a Django Rest Framework call and operate it like a dictionary (or something similar). Am I missing something, or is that not possible?

i.e.,

if the call to http://localhost:8000/api/1/roles/

yields

{"count": 2, "next": null, "previous": null, "results": [{"user": {"username": "smithb", "first_name": "Bob", "last_name": "Smith"}, "role_type": 2, "item": 1}, {"user": {"username": "jjones", "first_name": "Jane", "last_name": "Jones"}, "role_type": 2, "item": 1}]}

I would think something akin to http://localhost:8000/api/1/roles/0/user/username should return smithb.

Does this functionality exist or do I need to build it myself?

thumbtackthief
  • 6,093
  • 10
  • 41
  • 87
  • Is this a duplicate of your other question, or is there a difference bettwen the two cases?... http://stackoverflow.com/questions/24288320/control-output-of-django-rest-framework – Tom Christie Jun 18 '14 at 19:09
  • They're related but different. I thought of making them one but thought they were different enough and that I'd be yelled at if I put them together. I can't win. ;) This is about further querying the results (i.e., diving deeper into what I get) while the other was about changing the display output. – thumbtackthief Jun 18 '14 at 19:35
  • Oh that's cool. Just checking :) – Tom Christie Jun 18 '14 at 19:36

1 Answers1

1

It appears to be something you will have to build yourself. That said Django makes this kind of thing very easy. In URLS you can specify parts of the url path to pass to the view. You can catch the values using regex and then pass them into your views function.

Urls:
url(regex=r'^user/api/1/roles/(?P<usernumber>\w{1,50})/(?P<username>\w{1,50})/$', view='views.profile_page')
a request for http://domain/user/api/1/roles/0/username/

View:
def someApiFunction(request, usernumber=None ,username=None):
   return HttpResponse(username)

Some additional Resources:
https://docs.djangoproject.com/en/1.7/intro/tutorial03/#writing-more-views
Capturing url parameters in request.GET

Community
  • 1
  • 1
Ian Leaman
  • 135
  • 7