python - Accessing extra data in a sqlalchemy associated object -
the user
object can involved project
in many different ways, specified job
association table. in template file, i'm trying loop through of projects in current_user.projects
, each one, specify job
user in project... can't syntax right.
models
class project(db.model): id = db.column(db.integer(), primary_key=true) title = db.column(db.string(80)) users = db.relationship("job", back_populates="project") class user(db.model, usermixin): id = db.column(db.integer, primary_key=true) first_name = db.column(db.string(155)) last_name = db.column(db.string(155)) email = db.column(db.string(255), unique=true) password = db.column(db.string(255)) projects = db.relationship("job", back_populates="user") # defines user's role related each assigned project class job(db.model): __tablename__ = 'job' user_id = db.column(db.integer, db.foreignkey('user.id'), primary_key=true) project_id = db.column(db.integer, db.foreignkey('project.id'), primary_key=true) job = db.column(db.string(50)) user = db.relationship("user", back_populates="projects") project = db.relationship("project", back_populates="users") def __init__(self, user_id, project_id, job="client"): self.user_id = user_id self.project_id = project_id self.job = job
view
@app.route('/projects') @login_required def projects(): projects = project.query.join(job).filter_by(user_id=current_user.id).all() # current_user.projects same thing return render_template('project/projects.html', projects=projects)
template
<div class="accordion style-two gradient-bg"> <div class="panel-group" id="accordion-2"> {% project in projects %} <div class="panel panel-default"> <!-- accordian title --> <div class="panel-heading"> <div class="panel-title"> <a class="accordion-toggle collapsed" href="#{{ project.id }}" data-parent="#accordion-2" data-toggle="collapse" aria-expanded="false"> {{ project }} - needed here--> ({{ project.user[0].job }}) </a> </div> </div> <!-- accordian detail --> <div id="{{ project.id }}" class="panel-collapse collapse"> <div class="panel-body"> <div class="container"> <div class="row"> <p>lorem ipsum dolor sit amet, ei vis iriure phaedrum, sea no regione delicata sadipscing. minim imperdiet vix ad, pro ei utinam dicunt epicurei. vide affert debitis has ex. vel ut consul molestiae pertinacia. duo graeci dictas consulatu in, ei veniam singulis qui, decore numquam duo id.</p> <p>prima tritani veritus id pro, elitr hendrerit comprehensam ei per. doming integre aliquando has at. ea eam aeterno lobortis, ea est mutat laudem semper. pertinax urbanitas nec ne, quo aliquip voluptaria scriptorem ut. ut mel tractatos reprimique contentiones, adhuc nulla apeirian usu no, mel imperdiet molestiae abhorreant ut.</p> </div> </div> </div> </div> </div> {% endfor %} </div> </div>
as shown above, have project.users[0].job
show correct information because have 1 user in system. how can specify on template level i'm looking job associated the current_user
i'm looping through projects
? thanks!
as suggested in comments, easiest way query associated table begin with: projects = job.query.filter_by(user_id=current_user.id).all()
i have ready access each project
referenced (project.project
) , can details of job (project.job
).
Comments
Post a Comment