In SQLFORM, a field with an IS_IN_DB or IS_IN_SET validator with
multiple=True gets the multiple options widget. When you create a
list:reference field, by default the field automatically gets assigned an
IS_IN_DB validator (if you don't explicitly assign a validator). However,
that is only possible if the referenced table has already been defined --
otherwise it is not yet possible to create the validator. When you define
the tables in the opposite order, you don't get the default validator, and
therefore you don't get the associated widget either.
Anthony
On Wednesday, March 20, 2013 1:58:22 PM UTC-4, Ricardo Cárdenas wrote:
>
> I define two tables: category and product. Product can belong in multiple
> categories; M:M relationship expressed thus:
>
> db.define_table('category', Field('name'))
> db.define_table('product', Field('name'), Field('categories', 'list:reference
> category'))
>
> When I define the controller for adding a product to the database:
>
> def new():
> form = SQLFORM(db.product)
> return dict(form=form)
>
> It works fine. I get my Multiple Selection widget on the Product form. I
> can select multiple categories, as expected.
>
> But if I switch the order of the two define_tables statements, such that
> the product table is defined prior to the category table, I don't get the
> widget I expected -- just an empty input field. Is this expected behavior,
> or am I missing something? Is it better practice to rearrange my table
> definitions, or perhaps to explicit any widgets/requires after all table
> definitions? (I couldn't find a reference to this in chapter 6 of the book.)
>
> In my simplistic example, it's no big deal, but in other cases it would be
> less appealing to rearrange the table definitions. For example, if I wanted
> to base an M:M relationship off the auth_user table, I'd have to place the
> auth.define_tables after my define_tables.
>
> best regards -Ricardo
>
--
---
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/groups/opt_out.