Created
April 7, 2016 02:35
-
-
Save pawl/589c3fd158557139737a78d3c73bfec0 to your computer and use it in GitHub Desktop.
flask-admin testing field with length defined
This file contains hidden or 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 | |
from flask_sqlalchemy import SQLAlchemy | |
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'] = 'mysql://root:@localhost/test' | |
db = SQLAlchemy(app) | |
# Flask views | |
@app.route('/') | |
def index(): | |
return '<a href="/admin/">Click me to get to Admin!</a>' | |
class Tyre(db.Model): | |
__tablename__ = 'tyres' | |
tyre_id = db.Column(db.Integer, primary_key=True) | |
desc2 = db.Column(db.String(length=10)) | |
class TyreAdmin(sqla.ModelView): | |
column_display_pk = True | |
form_columns = ['desc2'] | |
# Create admin | |
admin = admin.Admin(app, name='Example: SQLAlchemy2', template_mode='bootstrap3') | |
admin.add_view(TyreAdmin(Tyre, 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