Commit 023addf3 authored by Petrus J.v.Rensburg's avatar Petrus J.v.Rensburg

Make sqla examples deployable.

parent 40b31cb9
...@@ -13,7 +13,7 @@ app = Flask(__name__) ...@@ -13,7 +13,7 @@ app = Flask(__name__)
app.config['SECRET_KEY'] = '123456790' app.config['SECRET_KEY'] = '123456790'
# Create in-memory database # Create in-memory database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sample_db_2.sqlite'
app.config['SQLALCHEMY_ECHO'] = True app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app) db = SQLAlchemy(app)
...@@ -22,6 +22,7 @@ db = SQLAlchemy(app) ...@@ -22,6 +22,7 @@ db = SQLAlchemy(app)
def index(): def index():
return '<a href="/admin/">Click me to get to Admin!</a>' return '<a href="/admin/">Click me to get to Admin!</a>'
class Car(db.Model): class Car(db.Model):
__tablename__ = 'cars' __tablename__ = 'cars'
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
...@@ -30,6 +31,7 @@ class Car(db.Model): ...@@ -30,6 +31,7 @@ class Car(db.Model):
def __unicode__(self): def __unicode__(self):
return self.desc return self.desc
class Tyre(db.Model): class Tyre(db.Model):
__tablename__ = 'tyres' __tablename__ = 'tyres'
car_id = db.Column(db.Integer, db.ForeignKey('cars.id'), primary_key=True) car_id = db.Column(db.Integer, db.ForeignKey('cars.id'), primary_key=True)
...@@ -37,19 +39,22 @@ class Tyre(db.Model): ...@@ -37,19 +39,22 @@ class Tyre(db.Model):
car = db.relationship('Car', backref='tyres') car = db.relationship('Car', backref='tyres')
desc = db.Column(db.String(50)) desc = db.Column(db.String(50))
class CarAdmin(sqla.ModelView): class CarAdmin(sqla.ModelView):
column_display_pk = True column_display_pk = True
form_columns = ['id', 'desc'] form_columns = ['id', 'desc']
class TyreAdmin(sqla.ModelView): class TyreAdmin(sqla.ModelView):
column_display_pk = True column_display_pk = True
form_columns = ['car', 'tyre_id', 'desc'] form_columns = ['car', 'tyre_id', 'desc']
# Create admin
admin = admin.Admin(app, 'Simple Models')
admin.add_view(CarAdmin(Car, db.session))
admin.add_view(TyreAdmin(Tyre, db.session))
if __name__ == '__main__': if __name__ == '__main__':
# Create admin
admin = admin.Admin(app, 'Simple Models')
admin.add_view(CarAdmin(Car, db.session))
admin.add_view(TyreAdmin(Tyre, db.session))
# Create DB # Create DB
db.create_all() db.create_all()
......
...@@ -16,7 +16,7 @@ app = Flask(__name__) ...@@ -16,7 +16,7 @@ app = Flask(__name__)
app.config['SECRET_KEY'] = '123456790' app.config['SECRET_KEY'] = '123456790'
# Create in-memory database # Create in-memory database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sample_db.sqlite'
app.config['SQLALCHEMY_ECHO'] = True app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app) db = SQLAlchemy(app)
...@@ -139,15 +139,16 @@ class TreeView(sqla.ModelView): ...@@ -139,15 +139,16 @@ class TreeView(sqla.ModelView):
inline_models = (Tree,) inline_models = (Tree,)
# Create admin
admin = admin.Admin(app, 'Simple Models')
# Add views
admin.add_view(UserAdmin(User, db.session))
admin.add_view(sqla.ModelView(Tag, db.session))
admin.add_view(PostAdmin(db.session))
admin.add_view(TreeView(Tree, db.session))
if __name__ == '__main__': if __name__ == '__main__':
# Create admin
admin = admin.Admin(app, 'Simple Models')
# Add views
admin.add_view(UserAdmin(User, db.session))
admin.add_view(sqla.ModelView(Tag, db.session))
admin.add_view(PostAdmin(db.session))
admin.add_view(TreeView(Tree, db.session))
# Create DB # Create DB
db.create_all() db.create_all()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment