Problem solved. My sample approach might help anybody with a similar 
problem. It might lack some additional checks and optimizations. Please add 
remarks to improve/simplify the code.

I left the model as is and added a separately populated drop-down to the 
controller and an IS_IN_SET validator instead of IS_IN_DB. 

def manage_packages():
    if request.args(0) == 'new' and request.args(1) == 'package':

        # Get available components
        components = [(r.id, r.some_field) for r in 
db(db.component).select()]

        form = SQLFORM.factory(
            db.package,
            Field(
                'components',
                requires=IS_IN_SET(components, multiple=True)
            )
        )

        if form.process().accepted:
            # Code to store the new package and associations to components

            package_id = 
db.package.insert(**db.package._filter_fields(form.vars))

            if package_id and form.vars.components:
                # Insert component package associations

                for component_id in form.vars.components:
                    existing_component = db.component(component_id)

                    if existing_component:
                        db.component_package_association.insert(
                            package_id=package_id,
                            component_id=existing_component
                        )

            response.flash = 'Package successfully created.'

        content = form
    else:
        content = SQLFORM.grid(db.package)

    return dict(content=content)


Am Dienstag, 9. Dezember 2014 17:50:42 UTC+1 schrieb Jan Beilicke:
>
>
>
>     db.define_table('component',
>                     Field('some_field'),
>                     format='%(some_field)s'
>                     )
>
>     db.define_table('package',
>                     Field('another_field'),
>                     format='%(another_field)s'
>                     )
>
>     db.define_table('component_package_association',
>                     Field('component_id', 'reference component'),
>                     Field('package_id', 'reference package'))
>
>     db.component_package_association.component_id.requires = IS_IN_DB(
>         db, 'component.id', '%(some_field)s', multiple=True)
>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to