Skip to content

Instantly share code, notes, and snippets.

@pawl
Created August 29, 2015 00:24
Show Gist options
  • Save pawl/ac76e8352fd1229c6411 to your computer and use it in GitHub Desktop.
Save pawl/ac76e8352fd1229c6411 to your computer and use it in GitHub Desktop.
import uuid
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import flask_admin as admin
from flask_admin.contrib import sqla
from sqlalchemy_utils import UUIDType
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
# Create in-memory database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sample_db_2.sqlite'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
# Flask views
@app.route('/')
def index():
return '<a href="/admin/">Click me to get to Admin!</a>'
class Car(db.Model):
__tablename__ = 'cars'
id = db.Column(UUIDType(binary=False), default=lambda: str(uuid.uuid4()), primary_key=True)
desc = db.Column(db.String())
datetime = db.Column(db.DateTime())
def __unicode__(self):
return self.desc
class CarAdmin(sqla.ModelView):
column_display_pk = True
column_filters = ('desc', 'datetime')
# Create admin
admin = admin.Admin(app, name='Example: SQLAlchemy2', template_mode='bootstrap3')
admin.add_view(CarAdmin(Car, db.session))
if __name__ == '__main__':
# Create DB
db.drop_all()
db.create_all()
# Start app
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment