Single row DataGridField

Hi

Is there any way to declare a DataGridField/Widget in such a way that it will render a single row table only on the AddForm and will not allow appending or inserting new rows?

If I set [] as the default value for DataGridField and disable auto_append, allow_insert, and allow_delete, no rows are created. If auto_append is enabled, more than one row may be added easily.

The only way I found is to disable auto_append, allow_insert, and allow_delete in DataGridWidget, but it only works if you pass a single row object as the default value for DataGridField. This solution seems incorrect.


class IRowSchema(Interface):

    field_set = schema.Set( 
                title='Field Set',
                value_type=schema.Choice(
                    vocabulary='voc',
                ),
                default=set(),
                required=True,)

     field_choice = schema.Choice(
                title='Choice field',
                vocabulary='voc2',
                required=True,
                )

class IMainSchema(model.Schema):

    directives.widget(
        "DGF",
        DataGridWidgetFactory,
        allow_insert=False,
        allow_delete=False,
        allow_reorder=False,
        auto_append=False)
    DGF = schema.List(
        title='Title',
        value_type=DictRow(schema=IRowSchema),
        required=False,
        default=[{'field_set': set(), 'field_choice': None'}]
        )

We use DGF quite a bit, went through my content types and forms to see how we solved this (Plone 5 and 6). Ours look very much the same :slight_smile: From a Plone 5 content type:

    # 20201020
    voc_info = schema.List(
        title       = _(u'VOC EU Grenzwert Kennzeichnung'),
        description = _(u'Art und Grenzwert für Flüchtige organische Verbindungen gemäß Richtlinie 2004/42/EG'),
        required    = True,
        default     = [{
            'voc_max_schedule' : u'--NOVALUE--',
            'voc_max_category' : u'',
            'voc_max_class'    : u'',
            'voc_max_content'  : 0,
        }],
        value_type  = DictRow(
            schema = IMaxVolatileCompoundsRowSchema
        ),
    )
    directives.widget('voc_info', VOCInfoGridFactory)
    dexteritytextindexer.searchable('voc_info')

@mtrebron Thanks!

That really works. I was confused that so far we pass preinitiated default object to the add form it always shows with failed fields validation message. Seems like you made your custom validation for voc_info field

It's just the widget with the custom directives that you have in IMainSchema

@adapter(IField)
@implementer(IFieldWidget)
def VOCInfoGridFactory(field, request):
    """
    A special widget constructor setting up widget parameters for DGF.
    """
    widget = DataGridFieldFactory(field, request)
    widget.auto_append = False
    widget.allow_insert = False
    widget.allow_delete = False
    widget.allow_reorder = False
    return widget

However, we use a customized version of GitHub - collective/collective.z3cform.dgftreeselect: Nested selection trees with z3c.form and data grid field for Plone to dynamically populate the grid.