Skip to content

Instantly share code, notes, and snippets.

Created November 24, 2014 22:18
Show Gist options
  • Save anonymous/6d57b898ee1ebfb31057 to your computer and use it in GitHub Desktop.
Save anonymous/6d57b898ee1ebfb31057 to your computer and use it in GitHub Desktop.
Example
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
@doobeh
Copy link

doobeh commented Nov 24, 2014

Prints:
(<3: Alice>, 2)
(<1: Bob>, 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment