Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django tastypie only fetch a particular field of a particular object

In tastypie my url: /api/v1/course/1/?format=json gives the following json:

{
created_on: "2012-02-27T08:00:54",
description: "this is course 1",
id: "1",
resource_uri: "/api/v1/course/1/",
subjects: [
    "/api/v1/subject/1/",
    "/api/v1/subject/2/"
],
title: "Course 1"
}

I want to do something like:

/api/v1/course/1/subjects/?format=json   

to get only the list of subjects for a given course. Is this possible?

like image 309
zubinmehta Avatar asked Feb 29 '12 08:02

zubinmehta


1 Answers

I'm guessing you want to do something like this where you specify a fields parameter so users can request only the fields they want. In your case, a user would send the request

/api/v1/course/1/?format=json&fields=subjects 

One way to implement this is to extend Tastypie to give you this functionality. Currently, the full_dehydrate method iterates over all fields and dehydrates each of them. You can add in a check to see if the user entered fields and if so, just skip the dehydrate phase for any fields that were not specified.

like image 96
Spike Avatar answered Nov 07 '22 20:11

Spike