I am starting to add Tastypie to a very small Django application I’m developing, and I was wondering if there is a way to send just the numeric id of a resource pointed to by a relationship, instead of the uri where the resource resides.
For instance, using one of the examples provided in the documentation:
The exposed “Entry” resource looks like:
{
"body": "Welcome to my blog!",
"id": "1",
"pub_date": "2011-05-20T00:46:38",
"resource_uri": "/api/v1/entry/1/",
"slug": "first-post",
"title": "First Post",
"user": "/api/v1/user/1/"
}
It has a relationship towards “user” that shows as "user": "/api/v1/user/1/". Is there any way of just making it "user": 1 (integer, if possible) so it looks like the following?
{
"body": "Welcome to my blog!",
"id": "1",
"pub_date": "2011-05-20T00:46:38",
"resource_uri": "/api/v1/entry/1/",
"slug": "first-post",
"title": "First Post",
"user": 1
}
I like the idea or keeping the resource_uri attribute as whole, but when it comes to modeling Sql Relationships, I’d rather have just the id (or a list of numeric ids, if the relationship is “ToMany“). Would it be good idea adding a dehydrate_user method to the EntryResource class to do this? It seems to work, but maybe there’s a more generic way of doing it (to avoid having to write a dehydrate method for every relationship)
Thank you in advance
You can try using hydrate dehydrate cycles
BUT I strongly recommend to stick with URI usage since it is how you can adress directly a resource. Hydrate and dehydrate are used for more complexe or virtual resources.