[Grok-dev] How do I use schema.Object?

Kevin Teague kevin at bud.ca
Tue Mar 10 20:11:13 EDT 2009


When you want to edit a schema.Object, you are generally wanting to
edit all of the fields of the interface that the schema.Object
conforms to. e.g. If IEvent defines a Boolean field and a Date field,
then you need to render those fields with Boolean and Date widgets
respectively.

When you use "form_fields = grok.AutoFields(INews)" Grok doesn't do
anything special to the schema.Object field. But you could say that if
a field is of type Object, and that field has a specific schema
supplied, then add all of the fields provided by that schema to the
list of form fields. That likely sounds a bit too magical for
grok.Fields() or grok.AutoFields() - I might just write-up my own
function such as AutoFieldsUnrollSubObjectFields().

Then I would provide an Adapter so that when the sub-fields from the
Object are edited, they delegate modifications to the sub-object as
desired. Somewhat like is described here:

http://grok.zope.org/documentation/tutorial/work-with-forms/using-multiple-schemas-with-a-form

class NewsEventAdapter(grok.Adapter):
    "Allows us to edit an Event via a News story"
    grok.context(News)
    grok.implements(IEvent)

    def _get_show_in_calendar(self): return
self.context.event.show_in_calendar
    def _set_show_in_calendar(self, value):
self.context.event.show_in_calendar= value
    show_in_calendar = property(_get_show_in_calendar,
_set_show_in_calendar)

    def _get_event_date(self): return self.context.event.event_date
    def _set_event_date(self, value): self.context.event.event_date =
value
    event_date = property(_get_event_date, _set_event_date)




More information about the Grok-dev mailing list