Last active
September 2, 2015 05:22
-
-
Save boolbag/9de8f3af7f31ce556791 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 | |
from flask_admin import Admin | |
from flask_admin.contrib import sqla | |
from flask_sqlalchemy import SQLAlchemy | |
from sqlalchemy import Column, ForeignKey, Integer, String | |
from sqlalchemy.orm import relationship | |
# Create application | |
app = Flask(__name__) | |
# Create dummy secrey key so we can use sessions | |
app.config['SECRET_KEY'] = '123456790' | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///a_sample_database.sqlite' | |
app.config['SQLALCHEMY_ECHO'] = True | |
db = SQLAlchemy(app) | |
# Create admin app | |
admin = Admin(app, name="Can't Delete Joe's Subscription to Time", template_mode='bootstrap3') | |
# Flask views | |
@app.route('/') | |
def index(): | |
return '<a href="/admin/">Click me to get to Admin!</a>' | |
class Person(db.Model): | |
__tablename__ = 'person' | |
uid = Column(Integer, primary_key=True) | |
username = Column(String(20), nullable=False, unique=True) | |
def __str__(self): | |
return self.username | |
class Magazine(db.Model): | |
__tablename__ = 'magazine' | |
mid = Column(Integer, primary_key=True) | |
title = Column(String(80), nullable=False, index=True) | |
def __str__(self): | |
return self.title | |
class Subscription(db.Model): | |
__tablename__ = 'subscription' | |
subid = Column(Integer, primary_key=True) | |
uid = Column(ForeignKey('person.uid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) | |
mid = Column(ForeignKey('magazine.mid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True) | |
notes = Column(String(140), nullable=False) | |
# need this to display in child tables (e.g. sub_schedule) | |
def __str__(self): | |
return self.notes | |
class SubscriptionSchedule(db.Model): | |
__tablename__ = 'subscription_schedule' | |
schedid = Column(Integer, primary_key=True) | |
# The deletion also fails when you remove the CASCADE | |
subid = Column(ForeignKey('subscription.subid', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) | |
notes = Column(String(140), nullable=False) | |
subscription = relationship('Subscription', backref='subschedule') | |
def __str__(self): | |
return self.notes | |
class SubscriptionAdmin(sqla.ModelView): | |
column_display_pk = True | |
class ScheduleAdmin(sqla.ModelView): | |
column_display_pk = True | |
admin.add_view(SubscriptionAdmin(Subscription, db.session)) | |
admin.add_view(ScheduleAdmin(SubscriptionSchedule, db.session, name='Schedule')) | |
# More submenu | |
admin.add_view(sqla.ModelView(Person, db.session, category='More')) | |
admin.add_view(sqla.ModelView(Magazine, db.session, category='More')) | |
if __name__ == '__main__': | |
db.drop_all() | |
db.create_all() | |
db.session.add(Person(uid=1, username='Joe')) | |
db.session.add(Person(uid=2, username='Ann')) | |
db.session.add(Magazine(mid=1, title='Time')) | |
db.session.add(Magazine(mid=2, title='Newsweek')) | |
db.session.commit() | |
db.session.add(Subscription(uid=1, mid=1, subid=1, notes='Joe subscribed to Time' )) | |
db.session.add(Subscription(uid=2, mid=2, subid=2, notes='Ann subscribed to Newsweek' )) | |
db.session.commit() | |
db.session.add(SubscriptionSchedule(schedid=1, subid=1, notes="Joe's Time subscription has a schedule" )) | |
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
Demonstrates this issue in flask-admin.