python - Flask SQLAlchemy - Access record data without column name -
long time reader, first time asker. i'm little unsure of how express trying accomplish. using flask
, sqlalchemy
, attempting put data record (in case, system.xxx) table. when use system.xxx
, column name, pictured below:
this code i'm using in jinja template:
{% block page_content %} <div class="page-header"> <h1>{{ system.serial }}</h1> </div> <hr width="80%"> <table class="table", border="5"> <tr> <th>status</th> <th>assignee</th> </tr> <tr> <td>{{ system.status }}</td> <td>{{ system.assignee }}</td> </tr> </table> {% endblock %}
how can access system.xxx
without getting 'status u
' or 'assignee u
' column info? i've spent literally hours on inter webs trying figure out, don't know how phrase question.
added: flask view.
@main.route('/system/<serial>', methods=['get']) def system(serial): system = system.query.filter_by(serial=serial).first_or_404() return render_template('system.html', system=system)
added: models in question
#table of individual systems class system(db.model): __tablename__ = 'systems' id = db.column(db.integer, primary_key=true) serial = db.column(db.string(64)) timestamp = db.column(db.datetime, index=true, default=datetime.utcnow) status_id = db.column(db.integer, db.foreignkey('statuses.id')) assignee_id = db.column(db.integer, db.foreignkey('assignees.id')) admin = db.column(db.string) def __repr__(self): return '<system %r>' % self.serial #table of assignees class assignee(usermixin, db.model): __tablename__ = 'assignees' id = db.column(db.integer, primary_key=true) name = db.column(db.string(64)) email = db.column(db.string(64), unique=true, index=true) systems = db.relationship('system', backref='assignee', lazy='dynamic') def __repr__(self): return '<assignee %r>' % self.name #table of status options class status(db.model): __tablename__ = 'statuses' id = db.column(db.integer, primary_key=true) name = db.column(db.string(64)) systems = db.relationship('system', backref='status', lazy='dynamic') @staticmethod def insert_statuses(): statuses = ['available', 'loaned', 'scrapped'] status in statuses: s = status(name=status) db.session.add(s) db.session.commit() def __repr__(self): return '<status %r>' % self.name
as thought you're accessing related status
, assignee
objects through relationship property in template, renders them using string representation. instead should access attributes want display:
<td>{{ system.status.name }}</td> <td>{{ system.assignee.name }} {{ system.assignee.email }}</td>
since you'll accessing relationships should consider eagerloading them.
Comments
Post a Comment