[Zope3-Users] Simple Add example for z3c.forms.subforms

Hermann Himmelbauer dusty at qwer.tk
Wed Jul 4 13:47:51 EDT 2007


Hi,
I had a look at the Addressbook demo, which is quite complicated, therefore I 
at first try to set up a very simple application, very similar to the example 
in the doctest subform.txt, where I have a main object (car) that has a 
subobject (owner), which are stored together.

So, basically I'd like to make a working example out of the doctest.

My problem is that the doctest seems to lack an important part, namely the 
creation of the subobject and the assignment to the parent object. Where 
would I do this?

I assume, I'd need a create() method - but where? In the subform? Or parent 
form? Or both - one for the sub-object, one for the parent?

I got it working like this in the parent object:

def create(self, data):
        newcar = Car(**data)
        newcar.owner = Owner(**self.owner.widgets.extract()[0])
	return newcar

However, I don't really like this **self.owner.widgets.extract()[0] statement, 
to my mind there should be a prettier way? Moreover, maybe the owner object 
should be created in the OwnerAddForm instead of the CarAddForm?

What I moreover don't understand in the subform.txt example is why the 
OwnerAddForm is of type form.EditForm instead of e.g. form.Form and the 
CarAddForm is not form.AddForm. This would also save the redefinition of 
updateWidgets(), I think.

So, what about this:

  >>> class OwnerAddForm(form.AddSubForm):
  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
  ...         'simple_owneredit.pt', templatePath)
  ...     fields = field.Fields(IOwner)
  ...     prefix = 'owner'

  >>> class CarAddForm(form.AddForm):
  ...     fields = field.Fields(ICar).select('model', 'make')
  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
  ...         'simple_caredit.pt', templatePath)
  ...     prefix = 'car'
  ...
  ...     def update(self):
  ...         self.owner = OwnerAddForm(None, self.request)
  ...         self.owner.update()
  ...         super(CarAddForm, self).update()
  ...
  ...     def create(self, data):
  ...         newcar = Car(**data)
  ...         newcar.owner = Owner(**self.owner.widgets.extract()[0])
  ...         return newcar


What do you think?

Best Regards,
Hermann

-- 
x1 at aon.at
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope3-users mailing list