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

Sebastian Ware sebastian at urbantalk.se
Wed Mar 11 08:45:17 EDT 2009


Fantastic! This works as advertised :)

One question, do you know how I get the invariants to work?

class IEvent(Interface):
     """
     An event in the event calendar.
     """
     show_in_calendar = schema.Bool(title=u"Visa i kalender")
     event_date = schema.Date(title=u'Eventdatum', required = False)

     @interface.invariant
     def show_and_date(event):
         if event.show_in_calendar and event.event_date is None:
             raise interface.Invalid("To show an event in the  
calendar, you need to specify a date!")



Mvh Sebastian

11 mar 2009 kl. 11.09 skrev Sylvain Viollon:

> Le Tue, 10 Mar 2009 15:00:02 +0100,
> Sebastian Ware <sebastian at urbantalk.se> a écrit :
>
>  Hello,
>
>> I am having difficulties understanding how to render forms using
>> schema.Object. I have looked at various Zope 3 samples, but I am
>> failing to translate it to Grok. The doctests I have found aren't
>> helping me either.
>>
>> Does anybody have some code that would successfully render and
>> update an object field?
>>
>> class IEvent(Interface):
>>     show_in_calendar = schema.Bool(title=u"Show in calendar")
>>     event_date = schema.Date(title=u'Date', required = False)
>>
>> class INews(IStory):
>>     event = schema.Object(title=u"Event", schema = IEvent)
>>
>>
>
>
>  In non-Grok I do the following, but I think this should work with
>  Grok as well:
>
>   class Event(persistent.Persistent):
>
>       def __init__(self, show_in_calendar, event_date):
>           self.show_in_calendar = show_in_calendar
>           self.event_date = event_date
>
>
>   from zope.app.form import CustomWidgetFactory
>   from zope.app.form.browser import ObjectWidget
>
>   class NewsForm(grok.Form):
>
>       form_fields = grok.Fields(INews)
>       form_fields['event'].custom_widget = \
>          CustomWidgetFactory(ObjectWidget, Event)
>
>
>   Best regards,
>
>   Sylvain
>
> -- 
> Sylvain Viollon -- Infrae
> t +31 10 243 7051 -- http://infrae.com
> Hoevestraat 10 3033GC Rotterdam -- The Netherlands



More information about the Grok-dev mailing list