If you think about it, this is not logically possible -- how can you set
values for the teacher dropdown that depend on the selected department
*before* the department has even been selected? Instead, you have to handle
this via Javascript -- once the user selects the department, then you
populate the list of teachers. There is no built-in support for that
functionality, but for some ideas, see
http://stackoverflow.com/questions/8146260/best-practice-for-populating-dropdown-based-on-other-dropdown-selection-in-web2p/8152910#8152910.
Anthony
On Saturday, August 1, 2015 at 6:38:21 AM UTC-4, mcamel wrote:
>
> Hi Anthony,
>
> Thanks for your reply.
>
> What i want to acheive is to restrict IS_IN_DB validator depending on the
> value of another field. It can be done on controllers, but i couldn't do it
> on model.
>
> Here is an example:
>
> - We have 2 departments: Letters and Numbers
> - We have teachers assgined to only one department
> - We have pupils assigned to only one department and optionally to a
> teacher, so we want to restrict the list of possible teachers to the
> department in common
>
>
> def index():
> db = DAL('sqlite:memory:')
> db.define_table('department',
> Field('name'), format='%(name)s')
> db.define_table('teacher',
> Field('department_id', 'reference department'),
> Field('name'))
> db.define_table('pupil',
> Field('department_id', 'reference department'),
> Field('teacher_id', 'reference teacher'),
> Field('name'))
> # populating...
> if not db(db.department).count():
> db.department.bulk_insert([dict(id=1, name='Letters'), dict(id=2,
> name='Numbers')])
> if not db(db.teacher).count():
> db.teacher.bulk_insert([dict(name='TeacherA', department_id=1),
> dict(name='Teacher1', department_id=2)])
> if not db(db.pupil).count():
> db.pupil.bulk_insert([dict(name='PupilA', department_id=1), dict(
> name='Pupil1', department_id=2)])
>
> db.pupil.teacher_id.requires = IS_EMPTY_OR(IS_IN_DB(db((db.teacher.
> department_id==db.pupil.department_id)),
> 'teacher.id',
> '%(name)s', zero=T('Choose one')))
> form = SQLFORM.grid(db.pupil, user_signature=False)
>
> return dict(form=form)
>
> This doesn't work as intended becuase it lets you assign teachers from
> both departments. I guess i'm doing a cartesian product instead of a
> filter...
>
> If you replace
> (db.teacher.department_id==db.pupil.department_id)
>
> with
> (db.teacher.department_id==request.post_vars.department_id)
>
> then you have an empty teachers list. Probably because post_vars is empty
> when you load the form?.
>
> If you try to use lambda as you can do with 'represent', then something
> weird happens and the dropdown dissapears:
> db.pupil.teacher_id.requires = lambda x,row: IS_EMPTY_OR(IS_IN_DB(db((
> db.teacher.department_id==row.department_id)),
> 'teacher.id',
> '%(name)s', zero=T('Choose one')))
>
> Maybe this cannot be done in model.
>
> Regards!
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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/d/optout.