Django tastypie gets only the specific field of a specific object

In tastypie my url is: / 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 a list of subjects for a given course. Is it possible?

+5
source share
3 answers

I assume that you want to do something like this , where you specify a parameter fieldsso that users can only request the fields that they want. In your case, the user will send a request

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

- Tastypie, . full_dehydrate . , , fields, , , .

+5

tastypie, :

http://django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships

class CourseResource(ModelResource):
    subjects = fields.ToManyField('myapp.api.resources.SubjectResource', 'subjects', full=True)
    class Meta:
        queryset = Course.objects.all()

class SubjectResource(ModelResource):
    course = fields.ToOneField(CourseResource, 'courses')

    class Meta:
        queryset = Subject.objects.all()
0

. django-tastypie django-tastypie-specific-fields, .

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

.

0
source

All Articles