Restricted Fields From Associated Models

19 views
Skip to first unread message

Adam

unread,
Sep 17, 2012, 4:45:51 AM9/17/12
to api-...@googlegroups.com
You might request data from a model limiting fields like the following:

/owners/{id}/?fields=name,age

However, when you want to find dogs belonging to that owner, and restrict those fields as well, I'm curious to know what the best practice is? Something like this:

/owners/{id}/dogs?fields[owners]=name,age&fields[dogs]=name,breed

Would this be accepted, or am I likely to head down a path with many non-joyous tears at the end?

Cheers,

Adam

Enrique Amodeo

unread,
Sep 17, 2012, 6:38:11 AM9/17/12
to api-...@googlegroups.com
I'd do the following:

/dogs -> All the dogs
/dogs/{dogid} -> Only the dog {dogid}
/owners/{id}/dogs -> All the dogs owned by {id}
/owners/{id}/dogs?fields=name,age -> All the dogs owned by {id}, give
me only the specified fields

And so on. Each dog have a link, with rel="owner" in its
representation, that points to the URI of its owner. No need to repeat
fields. I don't need the necessity of fields[owners]=name,age in the
querystring. But perhaps I'm missing something.

Cheers,
Enrique Amodeo
> --
> You received this message because you are subscribed to the Google Groups
> "API Craft" group.
> To unsubscribe from this group, send email to
> api-craft+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/api-craft?hl=en.
>
>

Adam

unread,
Sep 17, 2012, 11:19:33 AM9/17/12
to api-...@googlegroups.com

Thanks for replying Enrique.

Yeah, that's exactly the way I would have it as well, but what if the following should also return some data about the owner:

/owners/{id}/dogs

If it did, how would I specify restricted field criteria for both dogs and owners models?

Adam 

Enrique Amodeo

unread,
Sep 17, 2012, 11:51:18 AM9/17/12
to api-...@googlegroups.com
The trick is including the link to the owner in the dog
representation, this way there is no duplication of data. Assuming you
are using JSON, a dog could be represented like:
{
"name":"Scooby", "age":5, "race":"terrier".....
"links":[
{"rel":"self", "href":"http://server.com/dogs/33"},
{"rel":"owner", "href":"http://server.com/owners/01934"}
]
}
This way, if you want to know the data about the owner of the dog,
simple do a GET to the "owner" link.

Cheers,
Enrique Amodeo
Reply all
Reply to author
Forward
0 new messages