Created
July 16, 2013 11:41
-
-
Save mrjoes/6007994 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.ext.sqlalchemy import SQLAlchemy | |
from flask.ext import admin, wtf | |
from flask.ext.admin.contrib import sqlamodel | |
from flask.ext.admin.contrib.sqlamodel import filters | |
# Create application | |
app = Flask(__name__) | |
# Create dummy secrey key so we can use sessions | |
app.config['SECRET_KEY'] = '123456790' | |
# Create in-memory database | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite' | |
app.config['SQLALCHEMY_ECHO'] = True | |
db = SQLAlchemy(app) | |
class User(db.Model): | |
__tablename__ = 'users' | |
id = db.Column(db.Integer(10), primary_key=True) | |
title = db.Column(db.String(120)) | |
def __unicode__(self): | |
return self.title | |
class Profile(User): | |
__tablename__ = 'profile' | |
profile_id = db.Column(db.Integer(10), primary_key=True) | |
name = db.Column(db.String(32)) | |
user_id = db.Column(db.Integer(), db.ForeignKey(User.id), index=True) | |
user = db.relationship(User, backref=db.backref("profile", uselist=False), uselist=False) | |
def __unicode__(self): | |
return self.name | |
__mapper_args__ = { | |
'inherit_condition': (user_id == User.id), | |
} | |
class UserView(sqlamodel.ModelView): | |
form_columns = ('title', 'name') | |
if __name__ == '__main__': | |
# Create admin | |
admin = admin.Admin(app) | |
# Create DB | |
db.create_all() | |
# Add views | |
admin.add_view(sqlamodel.ModelView(User, db.session)) | |
admin.add_view(UserView(Profile, db.session)) | |
# Start app | |
app.debug = True | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment