문제

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

도움이 되었습니까?

해결책

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)))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top