Skip to content

Instantly share code, notes, and snippets.

@ftnext ftnext/
Last active Jun 12, 2018

What would you like to do?
Web Apps × FlaskでDBを使うときのベストプラクティス?を調べる(Azureもくもく会@新宿 No.20 2018/06/12)Blog:
# app/
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
myapp = Flask(__name__) # appだとフォルダ名と競合すると懸念した
db = SQLAlchemy(myapp)
from app import views
class Config(object):
SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db'
from app import myapp
# app/
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
def __repr__(self):
return '<User {}>'.format(self.username)
def init():
def retrieve_users():
users = User.query.all()
return [create_user_info(u) for u in users]
def create_user_info(user):
return {
'username': user.username,
# app/
from flask import jsonify
from app import myapp, db
from app.models import retrieve_users
def show_users():
return jsonify(retrieve_users())

This comment has been minimized.

Copy link
Owner Author

ftnext commented Jun 12, 2018


  • ├
  • ├
  • └ app/
    • ├
    • └


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.