Use of resource_uri as primary key vs. model object id

373 views
Skip to first unread message

Meetesh Karia

unread,
May 24, 2012, 4:09:38 AM5/24/12
to Tastypie
Hi all,

After years of using piston, I spent some time this week investigating
tastypie and really liked it. One aspect of it that I'm not sure I
fully understand how best to work with is the fact that the
resource_uri is used instead of the object id as a primary/foreign
key. For an app that doesn't cache any information on the client-side,
I can see how that greatly simplifies the client-side code and makes
it more robust. However, when writing a mobile app or using something
like backbone.js, it seems to require a lot more brittle, client-side
parsing to get the ID from the resource_uri.

Is there a standard way people deal with this when using tastypie?

Thanks,
Meetesh

Bruno Rocha

unread,
May 24, 2012, 11:28:55 AM5/24/12
to django-...@googlegroups.com
In the response you also have the ID field, it does not solve the problem?

objects"[(20)
{
"city""Arujá",
"city_no_accent""Aruja",
"id""1",
"region""/v1/region/3/",
"resource_uri""/v1/city/1/",
"state""São Paulo",
"state_no_accent""Sao Paulo"
},
--

Josh Bohde

unread,
May 24, 2012, 11:43:20 AM5/24/12
to django-...@googlegroups.com
In backbone-tastypie (https://github.com/PaulUithol/backbone-tastypie), you generally use the `resource_uri` as the logical id, and rarely need to use the actual `id`. 

Meetesh Karia

unread,
May 24, 2012, 6:36:25 PM5/24/12
to django-...@googlegroups.com
Bruno - the problem is with the region field below. The reference uses the resource_uri rather than the id and the latter is what I'd want.

Josh - I was using backbone-tastypie and actually had to fork it to get it to work the way I wanted it to. Basically, I wanted to have an object id in the URL and I didn't want that to be the resource_uri. So, in my router, when I handle a details view (with the object id in the URL), I only have access to the id and not the resource_uri. However, backbone-tastypie makes it such that models are keyed by resource_uri and not id in collections. So, looking up my model would require me to construct a resource_uri out of the id ...

Thanks,
Meetesh
Reply all
Reply to author
Forward
0 new messages