django tastypie only fetch a particular field of a particular object
Asked Answered
B

3

6

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?

Bostwick answered 29/2, 2012 at 8:34 Comment(3)
as json services does not require SEO I don't get the point. Or maybe you want all subjects related to you course.Legislative
django-tastypie.readthedocs.org/en/latest/…Legislative
yes, I just was wondering if in a more complicated nested json, can i just query for the entity I want. I just want the subjects, if possible. In reality this json is too big and I was thinking if there is a way to do this without writing a new resource.Bostwick
C
5

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.

Crosshatch answered 6/3, 2012 at 20:32 Comment(1)
Here is an extension that does just that: github.com/dan-klasson/django-tastypie-specified-fields. Although it does not yet support m2m.Wendywendye
L
0

The reverse relations are not created by default in tastypie but it looks pretty simple:

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()
Legislative answered 29/2, 2012 at 9:8 Comment(3)
No, I have done this exact same thing which shows subjects within the course json, as per the json posted in the question. I was wondering if I can just get the subjects only, or does my question not make sense? thanks.Bostwick
I added full=True, it's no more links but full objects... was it what you wanted? I'm discovering tastypie with you to answer so please be indulgent ^^.Legislative
you still may do 2 course ressource to have one with only subjects our have a subject ressource with a custom query_filter django-tastypie.readthedocs.org/en/latest/…Legislative
C
0

I had the same issue. I then implemented a very simple django-tastypie extension called django-tastypie-specific-fields which will help you to select the fields you are interested in. You will able to fetch your data with such a simple request

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

It will also give you much more possibilities.

Christianachristiane answered 28/9, 2016 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.