Skip to content

Instantly share code, notes, and snippets.

@boolbag
Created August 25, 2015 05:55
Show Gist options
  • Save boolbag/ec2bdd25ac5de0c12b63 to your computer and use it in GitHub Desktop.
Save boolbag/ec2bdd25ac5de0c12b63 to your computer and use it in GitHub Desktop.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import String, Enum
import flask_admin as admin
from flask_admin.contrib import sqla
# 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:///a_sample_database.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 Pet(db.Model):
__tablename__ = 'pet'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
petname = db.Column(String(20), nullable=False, unique=True)
favorite_day = db.Column(Enum('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'),
nullable=True)
class PetAdmin(sqla.ModelView):
column_editable_list = ('petname', 'favorite_day')
form_choices = {
'favorite_day': [ ('', '<leave empty>'), ('Monday', 'Monday'), ('Tuesday', 'Tuesday'),
('Wednesday', 'Wednesday'), ('Thursday', 'Thursday'), ('Friday', 'Friday'),
('Saturday', 'Saturday'), ('Sunday', 'Sunday'),]
}
# Create admin
admin = admin.Admin(app, name='Inline Enum Form Choices Issue', template_mode='bootstrap3')
admin.add_view(PetAdmin(Pet, db.session))
if __name__ == '__main__':
# Create DB
db.drop_all()
db.create_all()
db.session.add(Pet(petname='Flipper', ))
db.session.add(Pet(petname='Lassie', ))
db.session.add(Pet(petname='Jumbo', favorite_day='Monday'))
db.session.commit()
# Start app
app.run(debug=True)
@boolbag
Copy link
Author

boolbag commented Aug 25, 2015

Illustrates this issue in flask-admin.

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