-
-
Save mrjoes/7673265 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
Requirements: Postgresql, Flask-Admin, Flask-Sqlalchemy | |
How to run it: | |
createdb inline_bug | |
psql inline_bug -af schema.sql | |
python app.py | |
Go to /admin and try to edit the servers. One will work, one will not. |
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 | |
from flask.ext.sqlalchemy import SQLAlchemy | |
from flask.ext.admin import Admin | |
from flask.ext.admin.contrib.sqla import ModelView | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://localhost:5432/various' | |
# Sqlalchemy Reflect | |
db = SQLAlchemy(app) | |
db.metadata.reflect(bind=db.engine) | |
# Models | |
class Haver(db.Model): | |
__table__ = db.Table('havers', db.metadata, autoload=True) | |
class Belonger(db.Model): | |
__table__ = db.Table('belongers', db.metadata, autoload=True) | |
haver = db.relationship('Haver', backref="belongers") | |
from sqlalchemy.orm import configure_mappers | |
configure_mappers() | |
# Admin Views | |
class HaverView(ModelView): | |
#inline_models = (Belonger,) | |
form_create_rules = [ | |
'name', | |
'belongers' | |
] | |
form_edit_rules = form_create_rules | |
# Create Admin | |
admin = Admin(app, name="InlineBug") | |
admin.add_view(HaverView(Haver, db.session)) | |
if __name__ == '__main__': | |
app.debug = True | |
app.run() |
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
CREATE TABLE havers ( | |
id serial primary key, | |
name varchar(255) | |
); | |
CREATE TABLE belongers ( | |
id serial primary key, | |
haver_id serial not null references havers (id) on delete cascade, | |
name varchar(255) | |
); | |
insert into havers (name) values ('Works'),('Breaks'); | |
insert into belongers (name, haver_id) select 'Belonger', id as haver_id from havers where name = 'Works'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment