发一个经典的Flask-SQLAlchemy使用场景

from flask import Flask

from flask.ext.sqlalchemy import SQLAlchemy


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

db = SQLAlchemy(app)



class User(db.Model):

    id = db.Column(db.Integer, primary_key=True)

    username = db.Column(db.String(80), unique=True)

    email = db.Column(db.String(120), unique=True)


    def __init__(self, username, email):

        self.username = username

        self.email = email


    def __repr__(self):

        return '<User %r>' % self.username


from datetime import datetime



class Post(db.Model):

    id = db.Column(db.Integer, primary_key=True)

    title = db.Column(db.String(80))

    body = db.Column(db.Text)

    pub_date = db.Column(db.DateTime)


    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))

    category = db.relationship('Category',

        backref=db.backref('posts', lazy='dynamic'))


    def __init__(self, title, body, category, pub_date=None):

        self.title = title

        self.body = body

        if pub_date is None:

            pub_date = datetime.utcnow()

        self.pub_date = pub_date

        self.category = category


    def __repr__(self):

        return '<Post %r>' % self.title



class Category(db.Model):

    id = db.Column(db.Integer, primary_key=True)

    name = db.Column(db.String(50))


    def __init__(self, name):

        self.name = name


    def __repr__(self):

        return '<Category %r>' % self.name


调用方法:


from yourapplication import db

db.create_all()



from yourapplication import User

admin = User('admin', '[email protected]')

guest = User('guest', '[email protected]')



db.session.add(admin)

db.session.add(guest)

db.session.commit()



users = User.query.all()

print users

admin = User.query.filter_by(username='admin').first()

print admin


你可能感兴趣的:(python,flask,sqlalchemy)