Setting Default Fields for Programmed Dexterity Elements

I have a Dexterity based content type plone.directives.form.Schemathat has several form hints for assigning default values:

@form.default_value(field=ITrial['start_on'])
def default_start_on(data):
    return datetime.now()

Some of the default options are more complex, passing back objects that are themselves instances of Dexterity types. These objects are needed to configure the main type caused by various events.

I'm testing now. Ideally, I would like to use something like:

item = createContentInContainer(folder, 'ctcc.model.trial', 'item')

That is, I would like the element to be selected by default without the need to manually pass to the constructor.

If I used zope.schema, I could use FieldProperty to set the proxy server in the schema fields. Is there something equivalent to Dexterity, or perhaps a function to push an object through creating a shape?

Solution: I ended up working with option David # 1, intercepting ObjectCreatedEvent.

@grok.subscribe(ITrial, IObjectCreatedEvent)
def create_trial(trial, event):            
    if getattr(trial, 'start_on', None) is None:
        trial.start_on = default_start_on(None)

It still seems like I'm replicating some of the behavior of the form, but at least it uses the same functions that the forms provide by default.

+5
source share
1 answer

As you discovered, the @ form.default_value decorator is respected by the z3c.form forms, but not when elements are created in other ways. You have several options:

  • ObjectCreatedEvent , . , , , .

  • Dexterity Generic Item . __init__, , . , .

  • . factory, . createContentInContainer IFactory , factory FTI. plone.dexterity.factory, , .

+1

All Articles