On Jun 20, 11:23 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> The code you have below works fine. I guess the question is how to do
> IS_IN_DB and IS_NOT_IN_DB at the same time for the same field. Is that
> the question?

Yes, that is the question.
Sorry, it was not stated explicitly.

On Jun 21, 12:36 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Correction!
>
> It can be done but in two steps:
>
> db.define_table('person',SQLField('name'))
> db.define_table('project',SQLField('name'))
> db.define_table('relation',
>   SQLField('person_id','reference person'),
>   SQLField('project_id','reference project'))
> db.relation.person_id.requires=IS_IN_DB(db,db.person.id,'%(name)s')
> db.relation.project_id.requires=[IS_IN_DB(db,db.project.id,'%(name)
> s'),
>   IS_NOT_IN_DB(db
> (db.relation.person_id==request.vars.person_id),db.relation.project_id)]
>
> because the last line sets 2 validators, there is no automatic
> dropbox. It must be added via a widget with a trick (clone the field)
>
> from gluon.html import OptionsWidget

from gluon.sqlhtml import OptionsWidget

> db.relation.project_id.widget=lambda f,v: OptionsWidget.widget(SQLField
> ('project_id',db.project,requires=db.relation.project_id.requires
> [0]),v)

Even with the correct import I get:

Traceback (most recent call last):
  File "C:\web2py\trunk\gluon\restricted.py", line 107, in restricted
    exec ccode in environment
  File "C:/web2py/trunk/applications/a1/controllers/default.py", line
92, in <module>
  File "C:\web2py\trunk\gluon\globals.py", line 100, in <lambda>
    self._caller = lambda f: f()
  File "C:/web2py/trunk/applications/a1/controllers/default.py", line
74, in add_relation
    form=SQLFORM(db.relation)
  File "C:\web2py\trunk\gluon\sqlhtml.py", line 509, in __init__
    inp = field.widget(field, default)
  File "C:/web2py/trunk/applications/a1/controllers/default.py", line
58, in <lambda>
    db.relation.project_id.widget=lambda f,v: OptionsWidget.widget
(SQLField
('project_id',db.project,requires=db.relation.project_id.requires
[0]),v)
  File "C:\web2py\trunk\gluon\sqlhtml.py", line 181, in widget
    attr = OptionsWidget._attributes(field, default, **attributes)
  File "C:\web2py\trunk\gluon\sqlhtml.py", line 48, in _attributes
    _id = '%s_%s' % (field._tablename, field.name),
AttributeError: 'SQLField' object has no attribute '_tablename'

I feel that the problem lies in the dual functionality of the
requires.
It is used  both as a widget source and as a validator (its intended
purpose).
I also tried defining the widget based on the IS_IN_DB validator and
then changing the requires to IS_NOT_IN_DB on the fly in the
controller but the widget keeps the original requires and the form
field is created with it so IS_NOT_IN_DB is never used.
You can probably go in and change the requires in the form but this is
getting needlessly complex.

>
> This should work but I will make it easier and more intuitive.

It is in your hands master ;-)

Denes.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to