knirirr
knirirr

Reputation: 2817

Adding extra fields to CKAN resources

I've been going through the documentation here to add some extra fields to datasets and resources in CKAN:

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

So far, everything is working as far as the "cleaning up the code" section, and my extra field shows up when editing the dataset. I've skipped the tags and moved on to adding custom fields to resources, but there's no sign of any ability to add a custom field to those. Here's the relevant part of plugin.py:

class ExampleIDatasetFormPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
  p.implements(p.IDatasetForm)
  p.implements(p.IConfigurer)

  def _modify_package_schema(self, schema):
    schema.update({
      'my_custom_field': [tk.get_validator('ignore_missing'),
                           tk.get_converter('convert_to_extras')]
    })
    schema['resources'].update({
      'my_custom_field' : [ tk.get_validator('ignore_missing')]
    })
    return schema

  def create_package_schema(self):
    schema = super(ExampleIDatasetFormPlugin, self).create_package_schema()
    schema = self._modify_package_schema(schema)
    return schema

  def update_package_schema(self):
    schema = super(ExampleIDatasetFormPlugin, self).update_package_schema()
    schema = self._modify_package_schema(schema)
    return schema

  def show_package_schema(self):
    schema = super(ExampleIDatasetFormPlugin, self).show_package_schema()
    schema.update({
      'my_custom_field': [tk.get_converter('convert_from_extras'),
                           tk.get_validator('ignore_missing')]
    })
    schema['resources'].update({
      'my_custom_field' : [ tk.get_validator('ignore_missing') ]
    })
    return schema

Can anyone suggest what might be going wrong?

Upvotes: 4

Views: 3208

Answers (2)

Wei Xu
Wei Xu

Reputation: 58

The plugin.py in this tutorial is right, but you also need to add a .html file called "resource_form.html" in the folder "templates/snippets/". Adding the following code to resource_form.html file:

{% ckan_extends %}

{% block basic_fields_url %}
{{ super() }}

  {{ form.input('custom_resource_text', label=_('Custom Text'), id='field-custom_resource_text', placeholder=_('custom resource text'), value=data.custom_resource_text, error=errors.custom_resource_text, classes=['control-medium']) }}
{% endblock %}

The tutorial forgot to talk about this, but if you refer to the source code in Github (https://github.com/ckan/ckan/tree/master/ckanext/example_idatasetform/templates/package/snippets), you can find what I am talking here!

Upvotes: 4

jaybrau
jaybrau

Reputation: 413

You are using the same name for your custom resource field as your custom extra (at the package level), though these would be completely distinct. You're not expecting this value to be shared are you? If not, how are you populating my_custom_field in resources, are you using the API, or have you customized the form? If it's not populated somehow, it will be quietly ignored.

Upvotes: 1

Related Questions