Skip to content

Instantly share code, notes, and snippets.

@mrjoes
Forked from nickretallack/README
Last active December 29, 2015 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrjoes/7673265 to your computer and use it in GitHub Desktop.
Save mrjoes/7673265 to your computer and use it in GitHub Desktop.
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.
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()
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