Massimo,
I will try and give you a translated example which is probable easier
to understand. I have two tables:
db.define_table('company',
db.Field('company_name',length=54,default='',notnull=True),
db.Field(...),
migrate=False)
db.define_table('activity',
db.Field
('activity_name',length=48,default='',notnull=True,unique=True),
db.Field(...),
migrate=False)
... and a combination table:
db.define_table('companyactivity',
db.Field
('company',db.company,default='',notnull=True,ondelete='CASCADE'),
db.Field
('activity',db.activity,default='',notnull=True,ondelete='RESTRICT'),
db.Field(...),
migrate=False)
To prevent the user to enter the same company activity combination
twice I would like to have a two column unique constraint on company
and activity. The first validator which I tried read like:
db.companyactivity.company.requires=[IS_IN_DB(db,db.company.id,'%
(company_name)s'),IS_NOT_IN_DB
(db.companyactivity.activity==request.vars.activity),db.companyactivity.company)]
db.companyactivity.activity.requires=IS_IN_DB(db,db.activity.id,'%
(activity_name)s')
This validator does not disable the drop box functionality, but
doesn't perform the correct validation as well. I can enter a company
activity combination twice.
In the second validator I put the validator on activity instead of
company, this disabled the drop box so I had to add that using the
widget.
db.companyactivity.company.requires=IS_IN_DB(db,db.company.id,'%
(company_name)s')
db.companyactivity.activity.requires=[IS_IN_DB(db,db.activity.id,'%
(activity_name)s'),IS_NOT_IN_DB
(db.companyactivity.company==request.vars.company),db.companyactivity.activity)]
tmp=SQLField
('activity',db.activity,requires=db.companyactivity.activity.requires
[0])
tmp._tablename=db.companyactivity.activity._tablename
db.companyactivity.activity.widget=lambda f,v: OptionsWidget.widget
(tmp,v)
This validator does display the drop box, but doesn't perform the
correct validation. I can enter a company activity combination twice.
I tried adding the line of code you suggested in your previous post,
but that did result in an error ticket being issued:
Traceback (most recent call last):
File "/Library/Python/2.5/site-packages/web2py/gluon/restricted.py",
line 176, in restricted
exec ccode in environment
File "/Library/Python/2.5/site-packages/web2py/applications/cms/
models/db.py", line 281, in <module>
db.bedrijfactiviteit.activiteit['requires']
=db.bedrijfactiviteit.activiteit.requires
TypeError: 'SQLField' object does not support item assignment
I am not sure the validator is the cause of the problem, I had a look
at the function I expose:
@auth.requires_membership('tier2_manager')
def crud_activity():
response.functionname='Manage activities'
db.companyactivity.company.writable=False
db.companyactivity.company.default=auth.user.company
form=create_form(db.companyactivity)
records=db((db.companyactivity.company==auth.user.company)&
(db.companyactivity.activity==db.activity.id))\
.select
(db.companyactivity.ALL,db.activity.activity_name,orderby=db.activity.activity_name)
return dict(form=form,records=records)
... maybe these lines of code are the cause of the problem
db.companyactivity.company.writable=False
db.companyactivity.company.default=auth.user.company
... request.vars.company probably doesn't have a value?
I hope I provided you with sufficient information to help me solve the
problem. I apologize for asking the question without translating the
Dutch words into English, to me German, Dutch and English are similar
one another.
Kind regards,
Annet.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---