Search code examples
pythonmysqlflaskormpeewee

Select One Column Using Peewee


I have selected one column by using the peewee, and then send it to the template. But nothing return.

I have one table named Entry, with column tag_name.

@app.route('/archive')
def tag():
    query_tag = (Entry.select(Entry.tag_name)).distinct())
    return object_list('t.html', query_tag, check_bounds=False)

The corresponding template:

{%block content %}
{% for tag in object_list %}
    <p>{{ tag }}</p>
{% endfor %}
{% endblock %}

And finally it display "None" result_photo

But if I change to below code, it can work,:

@app.route('/archive')
def tag():
    query_tag = (Entry.select().distinct())
    return object_list('t.html', query_tag, check_bounds=False)

And the template:

{%block content %}
{% for tag in object_list %}
    <p>{{ tag.tag_name }}</p>
{% endfor %}
{% endblock %}

Solution

  • You can combine your two examples and the following should work:

    query_tag = Entry.select(Entry.tag_name).distinct()
    

    And the template:

    {% for entry in object_list %}
        <p>{{ entry.tag_name }}</p>
    {% endfor %}
    

    Because, even though you've only selected one column, Peewee will still be returning Entry objects. The Entry objects will only have the "tag_name" field populated, though.