Created
July 31, 2015 02:04
-
-
Save pawl/96afa9e1edbbe6b7e021 to your computer and use it in GitHub Desktop.
Trying to reproduce issue #834 in flask-admin
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'] = '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 Person(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String(50)) | |
addresses = db.relationship('Address', backref='person', lazy='dynamic') | |
class Address(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
email = db.Column(db.String(50)) | |
person_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=True) | |
class PersonAdmin(sqla.ModelView): | |
column_display_pk = True | |
column_searchable_list = ['name', Address.email] | |
class AddressAdmin(sqla.ModelView): | |
column_display_pk = True | |
column_searchable_list = ['email', Person.name] | |
# Create admin | |
admin = admin.Admin(app, name='Example: SQLAlchemy2', template_mode='bootstrap3') | |
admin.add_view(PersonAdmin(Person, db.session)) | |
admin.add_view(AddressAdmin(Address, db.session)) | |
if __name__ == '__main__': | |
# Create DB | |
db.drop_all() | |
db.create_all() | |
db.session.add(Person(name="test")) | |
db.session.add(Person(name="blah")) | |
db.session.add(Address(email="[email protected]", person_id=1)) | |
db.session.add(Address(email="[email protected]", person_id=None)) | |
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