Last active
August 21, 2017 03:11
-
-
Save ntroutman/04cc330303ac73847520a1804ea58d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from application import app | |
from flask_sqlalchemy import SQLAlchemy | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' | |
db = SQLAlchemy(app) | |
"""Only called if the application has never been initialized. Probably called from the python shell.""" | |
def create_all(): | |
db.create_all() | |
def commit(): | |
db.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/home/austin/your-workspace/ | |
your_flask_app/ | |
__init__.py | |
database.py | |
app.py | |
user.py | |
main.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import database | |
from user import User | |
def add_some_users(): | |
admin = User('admin', 'admin@example.com') | |
guest = User('guest', 'guest@example.com') | |
admin.save() | |
guest.save(guest) | |
database.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from database import db | |
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(80), unique=True) | |
email = db.Column(db.String(120), unique=True) | |
def __init__(self, username, email): | |
self.username = username | |
self.email = email | |
def __repr__(self): | |
return '<User %r>' % self.username | |
def save(): | |
db.session.save(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment