In order to make many to many relation ship I make a middle table to combine two tables ,the tables like this:
db.define_table('problem',
Field('task_id','reference task'),
Field('title','string',unique=True,length=255))
db.define_table('task',
Field('title','string',unique=True,length=255),
Field('course_id','reference courses'))
db.define_table('belong',
Field('task_id','reference task'),
Field('problem_id','reference problem')
)
db.belong.task_id.requires=IS_IN_DB(db,'task.id','%(title)s')
db.belong.problem_id.requires=IS_IN_DB(db,'problem.id','%(title)s')
I use SQLFORM to insert into belong
table.I want there are no duplicate task and problem. suppose there is a record like (1,task1,problem1)
already exist in belong
table,then if I choose task1 in SQLFORM,how to make the problem_id dropdown list not to show problem1. I have found similar question here
and do some test like the following in db.py
:
test1:
db.belong.problem_id.requires=IS_NOT_IN_DB(db(db.belong.task_id==request.vars.task_id),'belong.problem_id').
test2:
db.belong.problem_id.requires=IS_NOT_IN_DB(db(db.belong.task_id==request.vars.task_id),'problem.id','%(title)s').
test3:
def my_form_processing(form):
a = form.vars.task_id
b=form.vars.problem_id
query=(db.belong.task_id==a)&(db.belong.problem_id==b)
if query :
form.errors.a= 'the record has existed'
def assignproblem():
form=SQLFORM(db.belong)
if form.process(onvalidation=my_form_processing).accepted:
response.flash='form accepted'
elif form.errors:
response.flash='form has errors'
else:
response.flash='please fill out the form'
return dict(form=form)
but it still not been solved.