0

I trying make app with is listing cities from pre-existing postgresql database. So far I able connect to database and make a query, but results are returned u'string',

Here my models.py

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Woonplaats(Base):
    __tablename__ = 'woonplaats'

    gid = Column(Integer, primary_key=True)
    woonplaatsnaam = Column(String(80))

    def __init__(self, woonplaatsnaam=None):
        self.woonplaatsnaam = woonplaatsnaam

    def __repr__(self):
        return  '%r' % (self.woonplaatsnaam)

And views.py

from flask import render_template, request
from app import app
from app import db
from app.models import Woonplaats

@app.route('/citylist')
def citylist():
    results = db.session.query(Woonplaats).all()
    print(results) #only for test
    return render_template('city.html', results = results)

1 Answer 1

2

In the absence of a __str__ (or __unicode__), a class's __repr__ will be used. When you print out the query set, you end up printing the __repr__ of the woonplaatsnaam column. To print out the value of the column instead, add a __str__ (or __unicode__) method.

def __str__(self):
    return self.woonplaatsnaam

# for Python 2.x
def __unicode__(self):
    return self.woonplaatsnaam
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.