0

I was following this tutorial as a starting point to using models/controllers with python. Bear with me as I'm learning python.

I'm basically trying to get a json representation of my database table with key:value pairs as one would typically expect.

I can't seem to figure out the proper way of doing this.

My Model looks like this:

from app import db
import json

# define a base model for other database tables to inherit
class Base(db.Model):
    __abstract__ = True

    id = db.Column(db.Integer,primary_key=True)
    date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
    date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())

# define a members model
class Member(Base):
    __tablename__ = "members"

    # member name
    fname = db.Column(db.String(48), nullable=False)
    sname = db.Column(db.String(48), nullable=False)
    title = db.Column(db.String(90), nullable=True)

    # new instance instantiation procedure
    def __init__(self, fname, sname, title):
        self.fname = fname
        self.sname = sname
        self.title = title

    def __repr__(self):
        # return '<Member %r>' % (self.fname)
        # return '[{"id":self.id,"fname":self.fname,"sname":self.sname,"title":self.title}]'
        return json.dumps(self.__dict__)

that will return an error: TypeError: datetime.datetime(2015, 2, 18, 11, 50, 1) is not JSON serializable

My Controller looks like the following:

# import flask dependencies
from flask import Blueprint, request, jsonify, Response

# import the database object from the main app module
from app import db

# import module models (i.e. Members)
from app.team_members.models import Member

# define the blueprint: 'member' set its url prefix: app.url/members
team_members = Blueprint('member',__name__,url_prefix='/members')

# set the route and accepted methods
@team_members.route('/list/',methods=['GET'])
def list():
    members = Member.query.all()

    return jsonify(dict(('member%d' % i, member) for i, member in enumerate(Member.query.all(), start=1)))
    # resp = Response(response=members, status=200, mimetype="application/json")
    # return resp

I understand that jsonify will not return a list, but a dictionary for security reasons. And If I understand correctly, I need __repr__ to return a string.

Any help would be much appreciated.

gin93r
  • 1,551
  • 4
  • 21
  • 39
  • Have you tried searching for a way to solve your datetime serialization issue? http://stackoverflow.com/questions/455580/json-datetime-between-python-and-javascript has some suggestions. Basically, use the isoformat string of the datetime objects to serialize them. – Dan Feb 19 '15 at 03:24
  • I did, and the the methods I tried didn't seem to work. I haven't come across that link yet though. I'll look more into it. Thanks. – gin93r Feb 19 '15 at 17:39
  • Even using the dthandler in the link, i still get `TypeError: "2015-02-18T11:50:01" is not JSON serializable` – gin93r Feb 19 '15 at 18:03

0 Answers0