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?

3
  • Is this a duplicate of your other question, or is there a difference bettwen the two cases?... stackoverflow.com/questions/24288320/… Commented Jun 18, 2014 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. Commented Jun 18, 2014 at 19:35
  • Oh that's cool. Just checking :) Commented Jun 18, 2014 at 19:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's the way I'd do it. Nice & simple and keeps you in control of things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.