python - Pyramid/SQLAlchemy displaying objects -


i'm creating record system, student can enrolled class.

# model class association(base): __tablename__ = 'association' class_id = column(integer, foreignkey('classes.id'), primary_key=true) student_id = column(integer, foreignkey('students.id'), primary_key=true) theclass = relationship("class")  class student(base): __tablename__ = 'students' id = column(integer, primary_key=true) name = column(string(30)) classlist = relationship("association", backref='student')  class class(base): __tablename__ = 'classes' id = column(integer, primary_key=true) name = column(string(20), nullable=false) teacher_id = column(integer, foreignkey('teachers.id')) enrolled_students = relationship("association") 

i want display of students not yet enrolled in class, i've used following code in program , template, displays of students on page.

currentclass = session.query(class).filter_by(id=class_id).first() students = session.query(student).all()  # template % st in students: % assoc in currentclass.enrolled_students: % if st.id != assoc.student_id:     <input type="checkbox" name="student_id" value="${ st.id }" >${ st.id } - ${ st.forename } ${ st.surname }</input><br/>  % endif % endfor % endfor 

i think code outputs each student name many times there students in current class (minus 1 if student enrolled in class) :)

your current logic is

for student in all_student:  # alice, bob, claude     enrolled_student in current_class.enrolled_students: # alice, bob         if student != enrolled_student:             print student 

the output of above be

alice, alice, bob, bob, claude, claude, claude   

(first iteration: alice == alice, skip. second: alice != bob, print. third: alice != claude, print, etc.)

also relationship setup not quite "idiomatic sqlalchemy". have @ http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#many-to-many if set models in example (e.q. using secondary parameter of relationship() function), you'll able like

% st in students: % if st not in class.enrolled_students:     <input type="checkbox" name="student_id" value="${ st.id }" >${ st.id } - ${ st.forename } ${ st.surname }</input><br/>  % endif % endfor 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -