Ryan Clark
Ryan Clark

Reputation: 311

Access package_id from resource dict in CKAN

I'm looking for a way to find the package_id for a resource in CKAN. I access the resource via the resource_show action:

resource_dict = toolkit.get_action("resource_show")(context, {"id": "my-resource"})

The resulting dict does not tell me what package the resource belongs to. It does tell me:

resource_dict.get("package_id", None) == None # <<< True
resource_dict["resource_group_id"] = "some-uuid"

And I can see in my database that a resource_group does have a package_id attribute. However, there doesn't seem to be a resource_group_show action.

Is there another way for me to navigate from a resource dict to its parent package?

Thanks!

Upvotes: 5

Views: 828

Answers (3)

Denis
Denis

Reputation: 890

In CKAN 2.3 and newer, package_id is returned in the call to the resource_show API call.

Upvotes: 2

reubano
reubano

Reputation: 5363

You have to use the revision_show method.

import ckanapi

ckan = ckanapi.RemoteCKAN('url', apikey='key')    
resource = ckan.action.resource_show(id='resource_id')
revision_id = resource['revision_id']
revision = ckan.action.revision_show(id=revision_id)
package_id = revision['packages'][0]

Upvotes: 0

jaybrau
jaybrau

Reputation: 413

You're correct, unfortunately the current version of CKAN (2.2.1) doesn't include the package_id when you call resource_show, and there is no resource_group_show call. If you are writing an extension, you can get around this by performing a direct database query:

SELECT resource_group.package_id
  FROM resource, resource_group
 WHERE resource.id='<resource_id>' AND
       resource.resource_group_id=resource_group.id;

If you're trying to get the package_id using the API, the only way to find it is to search from the top down, calling package_list, then package_show for each until you find the one containing the pertinent resource_id.

Upvotes: 0

Related Questions