Question

This is the resource

class TagResource(ModelResource):
    user = tastypie.fields.ForeignKey(UserResource,'user')

    class Meta:
        queryset = Tag.objects.all()
        resource_name = 'tag'
        authorization= Authorization()
        object_class = Tag
        filtering = { 
            'name' : ALL, 
        }

simple get request

http://localhost:8000/api/v1/tag/1/?format=json

returns with empty resource_uri

{"created": "2014-03-26T15:14:11.928068", 
 "id": 1, "name": "test", 
 "resource_uri": "", "user": ""}

Why is that ?

I tried

def hydrate_resource_uri(self, bundle): 
  return bundle.get_resource_uri()

It didn't work and i'm pretty sure it's not supposed to require special care.

What am i missing ?

Was it helpful?

Solution

I know this is old, but I know your problem, I just had it on mine, you have a "namespace" on the API URL include or on any URL includes further up in your URL tree.

OTHER TIPS

I had the same problem, and it was because I forgot to register my resource in the urls.py. Ensure you have something like this in your urls.py file:

myapi.register(TagResource())

I have created a blog for highlighting the same problem. link to my blog

Tastypie give couple of options to create a Model Resource.

  1. ModelResource
  2. NamespacedModelResource

When namespace is included in urls.py then Tastypie logic of generating resource_uri fails as it also expects the same namespace in the api urls. To overcome this problem either one has to remove the namespace from module level urls.py or implement namespace with Tastypie. First solution looks easy but it may break your application. The below code will help you use second approach.

Api.py

from tastypie.resources import NamespacedModelResource

class EntityResource(NamespacedModelResource):
    class Meta:
        queryset = Entity.objects.all()
        allowed_methods = ['get']
        resource_name = 'entity'
        authentication = SessionAuthentication()
        authorization = Authorization()

mymodule/url.py

from tastypie.api import NamespacedApi
from mymodule.api import EntityResource

v1_api = NamespacedApi(api_name='v1', urlconf_namespace='mymodule')
v1_api.register(EntityResource())

urlpatterns = patterns('',
    url(r'^api/', include(v1_api.urls)),
)

Make sure you use same namespace for module and its api urls. The above code will surely generate proper resource_uri.

Try to remove the object_class, I think that if this is ModelResource, you don't need this.

This may be because, for some reason, the api_name is missing. Try to add it in the resource meta. For instance, if your resource uri is /api/v1/YourResourceName, try to add in your resource Meta:

api_name = 'v1'

Hope it helps.

Only if someone else get this problem caused by a namespace in urls.py. You have to use NamespacedModelResource instead of ModelResource:

from tastypie.resources import NamespacedModelResource

class TagResource(NamespacedModelResource):
    user = tastypie.fields.ForeignKey(UserResource,'user')

    class Meta:
        queryset = Tag.objects.all()
        resource_name = 'tag'
        authorization= Authorization()
        object_class = Tag
        filtering = { 
            'name' : ALL, 
        }

and then into your module's urls.py

from tastypie.api import NamespacedApi

v1_api = NamespacedApi(api_name='v1', urlconf_namespace='your_module')
v1_api.register(TagResource())

urlpatterns = patterns(
    '',
    url(r'^api/', include(v1_api.urls)),
)

Check this post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top