I'm trying to use a sqlalchemy loading strategy to speed up my queries. After reading this I realized that I was making the mistake of looping through the records in my template. The only problem is that i get this error:
NameError: global name 'joinedload' is not defined.
Is this happening because I'm using flask-sqlalchemy or em I forgetting to import something?
Models.py:
inspection_violations = db.Table('inspection_violations',
db.Column('violation_id', db.Integer, db.ForeignKey('violations.violation_number')),
db.Column('inspection_id', db.Integer, db.ForeignKey('inspection.inspection_id')), )
class Inspection(db.Model):
inspection_id = db.Column(db.Integer, primary_key=True)
violations = db.relationship('Violations', secondary=inspection_violations, backref=db.backref('inspection', lazy='dinamic'))
facility_id = db.Column(db.String(100), db.ForeignKey('facilities.branch_name'))
class Violations(db.Model):
violation_number = db.Column(db.Integer, primary_key=True)
details = db.Column(db.Text)
Violation Blueprint:
@violations_blueprint.route('/violations/<int:violation_number>')
def show_single_violation(violation_number):
violation = Violations.query.options(joinedload('inspection')).get(violation_number)
return render_template('violations/single-violation.html' ,violation=violation)
Template:
{% for inspection in violation.inspection %}
<p><a href="{{ url_for('inspection_blueprint.show_inspection', id=inspection.inspection_id) }}"> {{ inspection.facilities.branch_name }}</a></p>
{% endfor %}
To give some context to my models I have a inspection record. Every inspection has one or more violations. And every violation has many inspections
Well, the error message says joinedload
is not defined, so the obvious solution would be to check if you imported it and if not -
from sqlalchemy.orm import joinedload
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With