[ckan-dev] Issues with adding a custom input field

Adrià Mercader adria.mercader at okfn.org
Mon Jan 7 16:16:35 UTC 2013


Hi Fabian,

When storing extra fields against the default CKAN schema, these get
internally stored as extras, which can hold arbitrary key-value pairs.
You need to add the following converters to your custom schema on your
form_to_db_schema and db_to_form_schema methods:


from ckan.logic.converters import convert_to_extras, convert_from_extras

class YourPlugin(IDatasetForm):

def form_to_db_schema(self):

 // get the default package schema
 //...

   schema['release_date'] = [convert_to_extras] //You can add more
validators if needed

   return schema

def db_to_form_schema(self):

 // get the default package schema
 //...
   schema['release_date'] = [convert_from_extras]

   return schema

After doing this, on additional_info.html, pkg_dict should have your
extra fields (pkg_dict.release_date).

Here is a real example of a custom schema that stores new fields (look
for those with convert_to_extras):

https://github.com/okfn/ckanext-harvest/blob/2.0-dataset-sources/ckanext/harvest/logic/schema.py


Hope this helps,

Adrià

On 7 January 2013 10:57, Kirstein, Fabian
<fabian.kirstein at fokus.fraunhofer.de> wrote:
> Hello,
>
>
>
> do you need any additional information to answer my question? I can’t solve
> it by my own.
>
> I tried to save the inputted value with schema.update and convert_to_extras
> methods, but I
>
> am not sure how to apply it.
>
>
>
> Thanks!
>
>
>
>
>
>
>
>
>
> Von: ckan-dev-bounces at lists.okfn.org
> [mailto:ckan-dev-bounces at lists.okfn.org] Im Auftrag von Kirstein, Fabian
> Gesendet: Donnerstag, 3. Januar 2013 12:25
> An: ckan-dev at lists.okfn.org
> Betreff: [ckan-dev] Issues with adding a custom input field
>
>
>
> Hello everyone,
>
>
>
> this thread follows my „Customizing Forms in 2.0a“  thread. I have still
> issues adding some custom input fields, store the input and display it.
>
> I try to explain what to do. I’ve started to write my own custom extension
> implementing IDatasetForm . I’ve overwritten the
> package_metadata_fields.html and add
>
> an additional field, like this:
>
> {{ form.input('release_date', label=_('Release Date'),
> id='field-release-date', placeholder=_('2011-06-13'),
> value=data.release_date, error=errors.release_date,
> classes=['control-medium']) }}
>
> In the form_to_db_schema() method, I would like to store the value of the
> field as an extra field. I am not pretty sure how to access the inputted
> value.
>
> Then I want to override additional_info.html to display the stored value.
> But I’m not really sure how to pass data to such a snippet. The c-object
> which
>
> I can manipulate setup_template_variables() is not available in the snippet.
>
>
>
> I would really appreciate any help. I can’t be so hard to add a single input
> field, I guess I just have some misunderstandings.
>
> Thanks a lot.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev
>




More information about the ckan-dev mailing list