How to use dexterity/forms to add a list of custom objects?

I create a class IAgenda and set value_type=schema.Datetime() and it works. I can add a list of datetimes in my agenda object

class IAgenda(model.Schema):    
    dates = schema.List(
        title=_(u"label_dates", default=u"Dates"),
        description=_(u"help_dates", default=u"Enter dates"),
        value_type=schema.Datetime(),
        required=True,
    )

But when I tried to use a class IInterval on dates I got an error on save: 'unicode' object has no attribute 'year'.

class IInterval(Interface):
    start = schema.Datetime(
        title=_(u"label_start", default=u"Start"),
        description=_(u"help_start", default=u"Enter a start date"),
        required=True,
    )

    end = schema.Datetime(
        title=_(u"label_end", default=u"End"),
        description=_(u"help_end", default=u"Enter an end date"),
        required=True,
    )


class IAgenda(model.Schema):
    dates = schema.List(
        title=_(u"label_dates", default=u"Dates"),
        description=_(u"help_dates", default=u"Enter dates"),
        value_type=schema.Object(IInterval),
        required=True,
    )

If I change start and end fields to Int I got another error: "The system could not process the given value". So, what is the correct way to use dexterity/forms to add a list of custom objects?

Dear Silvio!

Had stuck in the same problem. I missed the factoryAdapterRegistration for z3c form.
Here a working example.

class ILiteral(model.Schema):
    """ Marker interfce and Dexterity Python Schema for Literal
    """

    text = schema.TextLine(
         title=_(u'Text'),
         required=True
    )

    language = schema.TextLine(
         title=_(u'Language'),
         required=False
    )


@implementer(ILiteral)
class Literal(Item):
    """
    """

from z3c.form.object import registerFactoryAdapter
registerFactoryAdapter(ILiteral, Literal)

class ICatalog(model.Schema):
    """ Marker interfce and Dexterity Python Schema for Catalog
    """

    add_title = schema.List(
         title=_(u'Translated Title'),
         required=False,
         value_type = schema.Object(ILiteral),
    )
1 Like

Thank you very much.

Please have a look at

https://community.plone.org/t/dexterity-object-as-field-of-another-dexterity-object-works-out-of-the-box-but-introspection-not/5419/4

since the proposed method above is not entirely correct.

Plone Foundation Code of Conduct