title: Python Flask SQLalchemy count rows of certain user
body: <p>I have a table rooms where user save rooms in. Each room has a foreign key <code>"users_id"</code> so I know which room belongs to which user.</p>
  
  <p>I need to count all rooms of each user and display the number for my admin dashboard.</p>
  
  <p>I have a solution but it feels not correct.</p>
  
  <p>I am making two queries for all rooms and all users:</p>
  
  <pre><code>users = db_session.query(User).order_by(desc('id'))
  
  rooms = db_session.query(Zimmer)
  </code></pre>
  
  <p>In my jinja2 I am the making loops to check which rooms belongs to which user. I am creating a list and append the rooms there for each user and then I use the jinja2 count method on the list variable, I also have to make the append variable <code>display: none;</code>:</p>
  
  <pre><code>{% for user in users %} 
      {% set total_rooms = [] %}
      {% for room in rooms %}
      {% if room.users_id == user.id %}
          &lt;span class="make-display-none"&gt;
          {{ total_rooms.append(room) }}
          &lt;/span&gt;
      {% endif %}
      {% endfor %}
      &lt;p&gt; Anzahl Objekte: {{ total_rooms|count }} &lt;/p&gt;    
  {% endfor %}
  </code></pre>
  
  <p>It works but I feel like there must be a better solution!</p>