Last active
March 22, 2021 22:35
-
-
Save ix5/1e63dd3e95a1b4547d9f00a74e5c06e0 to your computer and use it in GitHub Desktop.
This file contains 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 | |
import flask_admin as admin | |
from flask_sqlalchemy import SQLAlchemy | |
from flask_admin.contrib import sqla | |
# Create application | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = '123456790' | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' | |
app.config['SQLALCHEMY_ECHO'] = True | |
db = SQLAlchemy(app) | |
@app.route('/') | |
def index(): | |
return '<a href="/admin/">Click me to get to Admin!</a>' | |
entry_tags_table = db.Table( | |
"entry_tags", | |
db.Model.metadata, | |
db.Column("entry_id", db.Integer, db.ForeignKey("entry.id")), | |
db.Column("tag_id", db.Integer, db.ForeignKey("tag.id")), | |
) | |
class Entry(db.Model): | |
__tablename__ = "entry" | |
id = db.Column(db.Integer, primary_key=True) | |
title = db.Column(db.String) | |
tags = db.relationship("Tag", secondary=entry_tags_table, back_populates='entries') | |
class Tag(db.Model): | |
__tablename__ = "tag" | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.Unicode, nullable=False) | |
entries = db.relationship("Entry", secondary=entry_tags_table, back_populates='tags') | |
class EntryAdmin(sqla.ModelView): | |
pass | |
class TagAdmin(sqla.ModelView): | |
pass | |
class DefaultView(sqla.ModelView): | |
column_searchable_list = [ | |
"title", | |
"tags.name", # Important to specify .name! | |
] | |
admin = admin.Admin(app, name='Test', template_mode='bootstrap4', index_view=DefaultView(Entry, db.session)) | |
admin.add_view(TagAdmin(Tag, db.session)) | |
if __name__ == '__main__': | |
import sqlalchemy | |
print(sqlalchemy.__version__) | |
db.create_all() | |
app.run(debug=True) |
Author
ix5
commented
Mar 22, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment