IS_EMPTY_OR(IS_IN_DB incompatible with custom form?

48 views
Skip to first unread message

José L.

unread,
Aug 26, 2014, 3:13:35 PM8/26/14
to web...@googlegroups.com
Hi, I'm using a custom form to represent the fields of a table. This table has referenced fields.
If I use:
db.define_table('department',
    Field('dept_id'),
    Field('nombre'),format='%(nombre)s')

db.define_table('employee',
    Field('firstName'),
    Field('lastName'),
    Field('dept_id', db.department))

The table employee represents correctly the dept names. But if I do:


db.define_table('employee',
    Field('firstName'),
    Field('lastName'),
    Field('dept_id', db.department), requires=IS_EMPTY_OR(IS_IN_DB(db, db.department.id,
                                        '%(nombre)s')))

{{=form.custom.widget.dept_id}} shows the departament id instead of the name.
I need to add the "IS_EMPTY_OR" condition, I've also tried adding notnull=False without success.

Any hint?

Thanks.
José L.

Anthony

unread,
Aug 26, 2014, 4:49:17 PM8/26/14
to web...@googlegroups.com


On Tuesday, August 26, 2014 3:13:35 PM UTC-4, José L. wrote:
Hi, I'm using a custom form to represent the fields of a table. This table has referenced fields.
If I use:
db.define_table('department',
    Field('dept_id'),
    Field('nombre'),format='%(nombre)s')

db.define_table('employee',
    Field('firstName'),
    Field('lastName'),
    Field('dept_id', db.department))

The table employee represents correctly the dept names. But if I do:


db.define_table('employee',
    Field('firstName'),
    Field('lastName'),
    Field('dept_id', db.department), requires=IS_EMPTY_OR(IS_IN_DB(db, db.department.id,
                                        '%(nombre)s')))

I assume the above is a typo -- should be:

    Field('dept_id', db.department, requires=IS_EMPTY_OR(IS_IN_DB(db, db.department.id,
         
'%(nombre)s'))))

{{=form.custom.widget.dept_id}} shows the departament id instead of the name.
I need to add the "IS_EMPTY_OR" condition, I've also tried adding notnull=False without success.

Are you saying that in a create or update form, the select dropdown includes a list of IDs rather than names, or that in a grid or read-only form you see IDs rather than names? The former should not be the case, and when I try it, I see a list of names. If the latter is the problem, that is because you have not defined a "represent" attribute for the dept_id field (if you explicitly specify a "requires" argument when defining a reference field, you do not get the automatic "represent" attribute defined, so you have to define that explicitly as well).

Anthony

José L.

unread,
Aug 27, 2014, 3:20:30 AM8/27/14
to web...@googlegroups.com


El martes, 26 de agosto de 2014 22:49:17 UTC+2, Anthony escribió:


On Tuesday, August 26, 2014 3:13:35 PM UTC-4, José L. wrote:
Hi, I'm using a custom form to represent the fields of a table. This table has referenced fields.
If I use:
db.define_table('department',
    Field('dept_id'),
    Field('nombre'),format='%(nombre)s')

db.define_table('employee',
    Field('firstName'),
    Field('lastName'),
    Field('dept_id', db.department))

The table employee represents correctly the dept names. But if I do:


db.define_table('employee',
    Field('firstName'),
    Field('lastName'),
    Field('dept_id', db.department), requires=IS_EMPTY_OR(IS_IN_DB(db, db.department.id,
                                        '%(nombre)s')))

I assume the above is a typo -- should be:

    Field('dept_id', db.department, requires=IS_EMPTY_OR(IS_IN_DB(db, db.department.id,
         
'%(nombre)s'))))


Yes, it was a typo.
 
{{=form.custom.widget.dept_id}} shows the departament id instead of the name.
I need to add the "IS_EMPTY_OR" condition, I've also tried adding notnull=False without success.

Are you saying that in a create or update form, the select dropdown includes a list of IDs rather than names, or that in a grid or read-only form you see IDs rather than names? The former should not be the case, and when I try it, I see a list of names. If the latter is the problem, that is because you have not defined a "represent" attribute for the dept_id field (if you explicitly specify a "requires" argument when defining a reference field, you do not get the automatic "represent" attribute defined, so you have to define that explicitly as well).

Anthony

It was the latter case. It worked when using a create form but not with the view. I didn't know that requires removed the "representation", I can not recall reading that in the documentation either.  Now it works perfectly in both cases adding the represent attribute to the field definition:

represent
= lambda id, row: db.department._format % db.department(id)



Thanks very much for your help.

José L.
Reply all
Reply to author
Forward
0 new messages