Re: [Zope3-Users] want to use ObjectWidget and default add form
[the subject has really become: how can a custom widget access content objects and containers?] Hi, I'm trying to model a 1-to-1 relationship in zope3: a Driver class that contains an instance of a Car class. I model the car as a schema.Object inside the driver interface: class IDriver(Interface): car = Object( title = u'car', default = None, schema=ICar, required = False) and it seems that I need to define a custom widget in order to avoid a ComponentLookupError. I'd like to let the user select from all the cars in the application's container, and I can provide that list in the form class: from zope.formlib import form class DriverEdit(form.EditForm): def cars(self): parent=zapi.getParent(self.context) return [name for name,child in parent.items() if ICar.providedBy(child)] form_fields = form.Fields(IDriver) form_fields["car"].custom_widget = CarsListWidget but I don't know how to access the form instance from the widget class: from zope.app.form.browser.widget import SimpleInputWidget class CarsListWidget(SimpleInputWidget): def __call__(self): return '\n'.join([ '' % ('car'), '\n'.join(['\t%s' % (car) ### self.context is Object field, not DriverEdit form ###so "self.context.cars()" FAILS for car in self.context.cars()]), '', ]) I dont really need to access the form instance, but that seems to be a reasonable way to access the application's container (all drivers and cars are put into the same 'LimoService' container for now). So how can I access the list of cars from the widget? And does the answer differ in add forms vs edit forms? Thanks! john saponara wrote: thanks christophe. while looking for an example of how to use CustomWidgetFactory, i found a comment on the web about formlib replacing browser:editform/addform (at http://faassen.n--tree.net/blog/view/weblog/2005/09/06/0) and so i'm now using the instructions in zope/formlib/form.txt. unfortunately i still had to customize the car widget to avoid an error. for the car widget, i want to let the user select from the list of all car objects in the parent 'limoService' container, but when i do i get the error "Not enough context information to get parent" at the getParent call. my custom widget is: from zope.app.form.browser.widget import SimpleInputWidget class CarsListWidget(SimpleInputWidget): def cars(self): parent=zapi.getParent(self.context) return [name for name,child in parent.items() if ICar.providedBy(child)] def __call__(self): return '\n'.join([ '' % ('car'), '\n'.join(['\t%s' % (car) for car in self.cars()]), '', ]) and the full error (when trying to edit driver 'd1' via 'd1/edit.html') is: 2008-01-03T21:56:05 ERROR SiteError http://localhost:2020/mylimo/d1/edit.html Traceback (most recent call last): File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 133, in publish result = publication.callObject(request, obj) File "C:\Python24\Lib\site-packages\zope\app\publication\zopepublication.py", line 161, in callObject return mapply(ob, request.getPositionalArguments(), request) File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 108, in mapply return debug_call(obj, args) - __traceback_info__: zope.app.pagetemplate.simpleviewclass.SimpleViewClass from C:\pr\z3\lib\python\limoService\edit.pt instance at 0x0210EE50> File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 114, in debug_call return obj(*args) File "C:\Python24\Lib\site-packages\zope\formlib\form.py", line 770, in __call__ return self.render() File "C:\Python24\Lib\site-packages\zope\formlib\form.py", line 764, in render self.form_result = self.template() File "C:\Python24\Lib\site-packages\zope\app\pagetemplate\viewpagetemplatefile.py", line 83, in __call__ return self.im_func(im_self, *args, **kw) File "C:\Python24\Lib\site-packages\zope\app\pagetemplate\viewpagetemplatefile.py", line 51, in __call__ sourceAnnotations=getattr(debug_flags, 'sourceAnnotations', 0), File "C:\Python24\Lib\site-packages\zope\pagetemplate\pagetemplate.py", line 117, in pt_render strictinsert=0, sourceAnnotations=sourceAnnotations)() File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 271, in __call__ self.interpret(self.program) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 861, in do_defineMacro self.interpret(macro) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 909, in do_extendMacro definingName, exte
Re: [Zope3-Users] want to use ObjectWidget and default add form
thanks christophe. while looking for an example of how to use CustomWidgetFactory, i found a comment on the web about formlib replacing browser:editform/addform (at http://faassen.n--tree.net/blog/view/weblog/2005/09/06/0) and so i'm now using the instructions in zope/formlib/form.txt. unfortunately i still had to customize the car widget to avoid an error. for the car widget, i want to let the user select from the list of all car objects in the parent 'limoService' container, but when i do i get the error "Not enough context information to get parent" at the getParent call. my custom widget is: from zope.app.form.browser.widget import SimpleInputWidget class CarsListWidget(SimpleInputWidget): def cars(self): parent=zapi.getParent(self.context) return [name for name,child in parent.items() if ICar.providedBy(child)] def __call__(self): return '\n'.join([ '' % ('car'), '\n'.join(['\t%s' % (car) for car in self.cars()]), '', ]) and the full error (when trying to edit driver 'd1' via 'd1/edit.html') is: 2008-01-03T21:56:05 ERROR SiteError http://localhost:2020/mylimo/d1/edit.html Traceback (most recent call last): File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 133, in publish result = publication.callObject(request, obj) File "C:\Python24\Lib\site-packages\zope\app\publication\zopepublication.py", line 161, in callObject return mapply(ob, request.getPositionalArguments(), request) File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 108, in mapply return debug_call(obj, args) - __traceback_info__: zope.app.pagetemplate.simpleviewclass.SimpleViewClass from C:\pr\z3\lib\python\limoService\edit.pt instance at 0x0210EE50> File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 114, in debug_call return obj(*args) File "C:\Python24\Lib\site-packages\zope\formlib\form.py", line 770, in __call__ return self.render() File "C:\Python24\Lib\site-packages\zope\formlib\form.py", line 764, in render self.form_result = self.template() File "C:\Python24\Lib\site-packages\zope\app\pagetemplate\viewpagetemplatefile.py", line 83, in __call__ return self.im_func(im_self, *args, **kw) File "C:\Python24\Lib\site-packages\zope\app\pagetemplate\viewpagetemplatefile.py", line 51, in __call__ sourceAnnotations=getattr(debug_flags, 'sourceAnnotations', 0), File "C:\Python24\Lib\site-packages\zope\pagetemplate\pagetemplate.py", line 117, in pt_render strictinsert=0, sourceAnnotations=sourceAnnotations)() File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 271, in __call__ self.interpret(self.program) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 861, in do_defineMacro self.interpret(macro) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 909, in do_extendMacro definingName, extending) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 891, in do_useMacro self.interpret(macro) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 536, in do_optTag_tal self.do_optTag(stuff) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 521, in do_optTag return self.no_tag(start, program) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 516, in no_tag self.interpret(program) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 861, in do_defineMacro self.interpret(macro) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 957, in do_defineSlot self.interpret(block) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 949, in do_defineSlot self.interpret(slot) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 861, in do_defineMacro self.interpret(macro) File "C:\Python24\Lib\site-packages\zope\tal\talinterpreter.py", line 346, in interpret handlers[opcode](self, args) File "C:\Pyth
Re: [Zope3-Users] want to use ObjectWidget and default add form
john saponara a écrit : hi, please point me to an example showing how to use ObjectWidget with a default add form. in case there is no example, perhaps my failing attempt below could serve as one, once it's modified to work. [...] ComponentLookupError: ((0x03584090>, URL=http://localhost:2020/mylimo/@@+/action.html>), zope.app.form.interfaces.IInputWidget>, u'') 127.0.0.1 - - It seems you have no widget associated to your Object field. You should use less zcml and implement your own addform: driver_widget = CustomWidgetFactory(ObjectWidget, Driver) class DriverAddForm(AddForm): form_fields = Fields(IDriver) form_fields['car'].custom_widget = driver_widget def __init__(self, context, request): (...) def create(self.data): (...) Christophe ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users