Created
August 28, 2015 22:54
-
-
Save pawl/85acf65fb2f6957b1b68 to your computer and use it in GitHub Desktop.
flask-wtf breaking flask-admin forms with unique
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
import flask_wtf | |
import flask_admin as admin | |
from flask import Flask | |
from flask_sqlalchemy import SQLAlchemy | |
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 | |
app.config['CSRF_ENABLED'] = True | |
db = SQLAlchemy(app) | |
flask_wtf.CsrfProtect(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(db.String, unique=True) | |
street = db.Column(db.String) | |
class PetAdmin(sqla.ModelView): | |
form_base_class = flask_wtf.Form | |
# Create admin | |
admin = admin.Admin(app, name='Unique not working?', 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')) | |
db.session.commit() | |
# Start app | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment