Question

Can I attach extras to CKAN resources? I noticed that ckan.model.Resource has an extras property, and so I tried to make a resource using something like this:

resource = {
    "package_id": "my-package-id",
    "url": "http://google.com",
    "name": "Google",
    "extras": [
        { "key": "myextra", "value": "my extra content" }
    ]
}

from ckan.plugins import toolkit
toolkit.get_action("resource_create")(faked_context, resource)

I followed along as the logic function eventually ended up running package_update, but failed with ValidationError: {u' junk': u'The input field __junk was not expected.'}

Thanks!

Was it helpful?

Solution

You don't have to use the key "extras" for resource extras, just add whatever arbitrary key-value pairs you want as top-level keys in the resource dict, and they will be converted to resource extras. For example, I think this should work:

resource = {
    "package_id": "my-package-id",
    "url": "http://google.com",
    "name": "Google",
    "myextra": "my extra content"
}

At least, I just tried that over the API and it worked, so I think it'll work via the plugins toolkit like you're doing it as well. I guess this feature needs documenting, probably in the resource_create() docstring.

(Note: this does not work for packages, for adding custom fields to packages see the example_idatasetform extension that ships with CKAN.)

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