ajax and many-to-many

13 views
Skip to first unread message

mattynoce

unread,
Jan 13, 2009, 11:54:04 AM1/13/09
to web2py Web Framework
hi all. i've done a lot with ajax, but am building my first web2py
app, and i have a question. i've searched the newsgroup but i
apologize if i'm overlooking a post.

i work for a school, and i have tables for students, courses, and a m-
m called studentcourses. i'm building an enrollment page that will
take in a courseid and show enrolled and available students like:

enrolled
billy
suzy

available
john
jenny

and when you click on available, they bounce up to enrolled. this is
all working fine.

now it's time to update studentcourses based on the students in the
enrolled section. i grab them using jquery and i'm successfully
sending ids that are being parsed into a list by a web2py function.
but what i can't figure out is how to properly update the database to
handle that. should i be making a SQLFORM (that never gets viewed)?
should i be writing custom SQL like i'm used to in other apps?

currently the controller function just looks like this:
def enrollmentupdate():
ids = request.vars.id
return ids

where ids is a list like ['1','3']. eventually i will just return a 1
or 0 to indicate success. but somewhere before that i need to update
the studentcourses table (in the past i've iterated over ids and
concatenated SQL update/insert statements). does anyone have
suggestions?

thanks so much!

matt

mdipierro

unread,
Jan 13, 2009, 12:49:51 PM1/13/09
to web2py Web Framework
It seems to me the enrollmentupdate function needs to know both the
course_id and list of student ids.
For speed you should not update the entire list all the time. You are
better off creating two function:

def drop_student()
course_id=request.vars.course_id
student_id = request.vars.student_id
return db(db.studentcourses.student==student_id)
(db.studentcourses.course==course_id).delete()

def add_student()
course_id=request.vars.course_id
student_id = request.vars.student_id
drop_student()
return db.studentcourses.insert
(student=student_id,course=course_id)

Sorry I did not quite answer your question.

Massimo

mattynoce

unread,
Jan 13, 2009, 2:10:03 PM1/13/09
to web2py Web Framework
massimo, thanks for the reply. actually, for speed i wasn't updating
the entire list every time, i was only updating it upon a "submit" at
the end. so i wasn't doing an ajax call for every add or drop, i was
javascripting the add/drops and then doing one large submit at the
end.

but i think your delete and insert examples are enough to help me make
it happen. thanks for your time...

matt
Reply all
Reply to author
Forward
0 new messages