[ckan-dev] Adding additional fields to resources.

Milo Thurston milo.thurston at oerc.ox.ac.uk
Mon Jun 8 10:32:50 UTC 2015


This documentation discusses adding additional fields to datasets and resources:

http://docs.ckan.org/en/latest/extensions/adding-custom-fields.html

I’m interested in adding a field (a url for a thumbnail image) to resources only, and so have done this in a plugin based on that documentation:


def _modify_package_schema(self, schema):
        _convert_to_extras = toolkit.get_converter('convert_to_extras')
        _ignore_missing = toolkit.get_validator('ignore_missing')
        _url_validator = toolkit.get_validator('url_validator')
        schema['resources'].update({ 'image_url' : [ _ignore_missing, _url_validator, _convert_to_extras ] })
        return schema


def show_package_schema(self):
        schema = super(MyPlugin, self).show_package_schema()
        _convert_from_extras = toolkit.get_converter('convert_from_extras')
        _ignore_missing = toolkit.get_validator(‘ignore_missing')
        _url_validator = toolkit.get_validator('url_validator')
        schema['resources'].update({ 'image_url' : [ _ignore_missing, _url_validator, _convert_from_extras ] })
        return schema

The following changes to template files were made:

templates/package/resource_read.html:

<tr>
<th scope="row">Image URL</th>
<td>{{ h.format_resource_items(res.image_url) or 'None' }}</td>
</tr>

templates/package/snippets/resource_form.html:

{{ form.input('image_url', label=_('Image URL'), id='field-image_url', placeholder=_('URL for a custom image for this resource'), value=data.image_url, error=errors.image_url, classes=['control-medium']) }}

The url validation on the form works, but any field I submit is not saved. Could this be because my code above is somehow faulty, or, is schema[‘resources’].update doing nothing? If anyone knows, please let me know. Thanks!


More information about the ckan-dev mailing list