Question

Consider 2 tables in the web2py python web framework.

The default auth_user

and this one:

b.define_table(
    'artwork',
    Field('user_id', 'reference auth_user', required=False),
    Field('name'),
    Field('description', 'text')
    )

Now, when I go to the Database Administration in (appadmin), I would expect the user_id to be optional. If I let the selection drop down empty, when manually entering an entry in the artwork table, it says : "value not in database" which seams against the required=False statement.

I would like to be able to insert an artwork entry without user_id

Can someone help me resolve this one? Thx a lot

Was it helpful?

Solution

When you create a reference field, by default it gets a requires=IS_IN_DB(...) validator. The requires attribute is enforced at the level of forms, whereas the required attribute is enforced at the level of the DAL. To override the default form validator, you can do:

Field('user_id', 'reference auth_user', requires=None)

or alternatively,

Field('user_id', 'reference auth_user',
      requires=IS_EMPTY_OR(IS_IN_DB(db, 'auth_user.id',
                                    db.auth_user._format)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top