Tastypie return empty resource_uri to get

This is a 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 request

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

returns empty resource_uri

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

Why?

I tried

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

This did not work, and I am sure that this should not require special care.

What am I missing?

+4
source share
6 answers

I know this is old, but I know your problem, I just used it on my own, you have a " namespace " in the API URL or at any URL, including the further URL of your URL tree.

+3
source

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

myapi.register(TagResource())
+2

.

Tastypie .

  • ModelResource
  • NamespacedModelResource

urls.py, Tastypie resource_uri , api url. , urls.py Tastypie. , . .

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)),
)

, api url. , , _.

+2

object_class, , ModelResource, .

0
source

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

api_name = 'v1'

Hope this helps.

0
source

Only if someone else gets this problem caused by the namespace in urls.py. You should 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 urls.py module

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)),
)

Mark this post.

0
source

Source: https://habr.com/ru/post/1533575/


All Articles