Answer the question
In order to leave comments, you need to log in
How to select fields from database to create site menu without unnecessary columns in Flask-SQLAlchemy?
I have such a function to select all posts to generate a menu
@app.context_processor
def menu():
articles = Article.query.filter_by(onoff=1).all()
return dict(articles=articles)
Answer the question
In order to leave comments, you need to log in
I'm not sure if this code will work wrapped with Flask, but in SQLAlchemy it's done something like this:
Such an instruction will return a list of tuples of the form:
[("name1", "url1", "num1"), ("name2", "url2", "num2"), ... ]
from sqlalchemy.orm import load_only
@app.context_processor
def menu():
articles = Article.query.filter_by(onoff=1).options(load_only("name", "url", "num"))
return dict(articles=articles)
{% for article in articles %}
<li><a href="/{{ article.url }}/">{{ article.article }}</a></li>
{% endfor %}
{% for article in articles %}
<li><a href="/{{ article.url }}/">{{ article.article|safe }}</a></li>
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question