Reputation: 197
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
Upvotes: 2
Views: 1353
Reputation: 19290
@Anthony's answer was right, however, nowadays it is no longer needed to explicitly write the IS_EMPTY_OR(...)
part for a referenced field, because it would be set automatically since web2py 2.16.x or later.
Upvotes: 0
Reputation: 25536
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)))
Upvotes: 6