Question

Using Flask-Admin with Mongoengine, I am stuck when trying to customize model view for the list of submitted posts. The idea is to add a cell to each post (corresponding to a row in the list) in order to show the number of comments submitted on each post.

I have added the following get method to the class:

class PostView(ModelView):

   def get(self):
       posts = Post.objects.all()
       return render_template('admin/model/list.html', posts=posts)

The list.html contains the following:

<td>
    {% for d in posts %}
        {% with total=d.comments | length %}
            {{ total }}
        {% endwith %}
    {% endfor %}
<td>

The table cells stay empty. What should I do instead ? Thanks in advance !

Was it helpful?

Solution

As I understand you trying create own view but I can't find any get method in ModelView.

Anyway flask-admin have flexible inheritance structure. So you can try just:

class PostView(ModelView):
    list_template = 'admin/model/posts-list.html'

templates/admin/model/posts-list.html:

{% extends 'admin/model/list.html' %}

{% block list_header %}
    {{ super() }}
    <th>Comments count</th>
{% endblock %}

{% block list_row %}
    {{ super() }}
    <td>{{ row.comments|length }}</td>
{% endblock %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top