[Grok-dev] Add forms - one object type to multiple object types

Darryl Cousins darryl at darrylcousins.net.nz
Sat Mar 10 18:54:28 EST 2007

On Sun, 2007-03-11 at 12:45 +1300, Darryl Cousins wrote:
> Hi,
> I'm trying out a simple site type app with grok.
> I've 3 content types: Site, Section and Page. Add form for page begins::
> class Add(grok.AddForm):
> 	grok.context(Site)
> 	grok.name('addpage')
> So now I can add pages to Sites. But I want also to add them to
> Sections. So the Add class needs a different context:
> grok.context(Section).
> It seems that I need to make a new add class in order to do so, one for
> Site and the other for Section.
> Using zcml I would have been able to register the add class for both
> Sites and Sections. Here (as I see from my low stand point) it seems not
> so straight forward.
> Thoughts?
> Kind regards,
> Darryl

Hi again,

Actually it wasn't so bad as I thought. I can re-use the grok.name
(things start falling into place).

So having the 2 separate add classes works like this:

class Add(grok.AddForm):

    form_fields = grok.Fields(
    form_fields += grok.AutoFields(Page)

    @grok.action(_('Add page'))
    def add(self, id, **data):
        new = Page(**data)
        self.context[id] = new

    @grok.action(_('Cancel'), validator=always_good)
    def cancel(self, **data):

class AddToSite(Add):

class AddToSection(Add):


More information about the Grok-dev mailing list