Skip to content

Instantly share code, notes, and snippets.

Created November 24, 2014 22:18

Revisions

  1. @invalid-email-address Anonymous created this gist Nov 24, 2014.
    34 changes: 34 additions & 0 deletions untitled.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
    from sqlalchemy import func

    app = Flask(__name__)
    app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite://'

    db = SQLAlchemy(app)

    class TestModel(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String())

    def __repr__(self):
    return "<{}: {}>".format(self.id, self.name)


    @app.route('/')
    def hello_world():
    return 'Hello World!'


    if __name__ == '__main__':
    with app.app_context():
    db.create_all()
    bob = TestModel(name="Bob")
    alice = TestModel(name="Alice")
    alice_two = TestModel(name="Alice")

    db.session.add_all([bob, alice, alice_two])
    db.session.commit()
    u = db.session.query(TestModel, func.count(TestModel.id)).group_by(TestModel.name).all()
    for x in u:
    print x