title: Flask-SQLAlchemy iterate column values on a single row
body: <p>I'm new to Flask and Python so apologies in advance. I'm using Flask-SQLAlchemy to return a database row, this all works fine:</p>
  
  <pre><code>customer = Customers.query.filter_by(cat_id = page).first()
  return render_template('test.html',
      customer = customer
      )
  </code></pre>
  
  <p>My problem is I'm trying to figure out how to then display the column values for this row in my jinja template using a loop. Is this the best way to approach this problem? I'm getting the "object is not iterable" error which I kind of understand but I'm not sure how to work around it. </p>
  
  <p>In my template I'm currently using:</p>
  
  <pre><code>{{customer.id}}
  {{customer.name}}
  {{customer.area}}
  etc.
  </code></pre>
  
  <p>But I would like to be doing something like this:</p>
  
  <pre><code>{% for item in customer %}
      {{item[column]}}
  {% endfor %}
  </code></pre>
  
  <p>Could the query be converted to a dictionary?</p>
  
  <p>I've searched all over trying to figure this out with no luck, this leads me to think I may be on the wrong track. </p>
  
  <p>Any advice much appreciated.
  <hr>
  <strong>Update:</strong><br>
  I think this is progress. The main change is in views.py where I've added <code>.__dict__</code> which from what I've read accesses the internal <code>__dict__</code> of a SQLAlchemy object. The for template loop now outputs the column values but it also outputs lots of other undesired stuff. Is there anyway to clean this up?</p>
  
  <p>models.py</p>
  
  <pre><code>class Customers(db.Model):
      id = db.Column(db.Integer, primary_key = True)
      cust_name = db.Column(db.String(64))
      cust_area = db.Column(db.String(64))
      cat_id = db.Column(db.Integer(8), index = True)
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>customer = Customers.query.filter_by(cat_id = page).first()
  return render_template('test.html',
      customer = customer.__dict__
      )
  </code></pre>
  
  <p>test.html</p>
  
  <pre><code>{% for key, value in customer.items() %}
      {{ key }} , {{ value }}
  {% endfor %}
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>cust_name , John _sa_instance_state , 
  &lt;sqlalchemy.orm.state.InstanceState object at 0x03961D50&gt; id , 1 cat_id , 2 cust_area , England
  </code></pre>