python sqlalchemy模型包

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python sqlalchemy模型包相关的知识,希望对你有一定的参考价值。

from sqlalchemy import Column, String, Integer
from passlib.hash import pbkdf2_sha256
from . import Base


class User(Base):

    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    first_name = Column(String(50))
    last_name = Column(String(50))
    username = Column(String(50))
    password = Column(String(150))

    def __init__(self, username, password, first_name='', last_name=''):
        self.first_name = first_name
        self.last_name = last_name
        self.username = username
        self.password = pbkdf2_sha256.hash(password)

    def __repr__(self):
        return '<User "{} {}">'.format(self.first_name, self.last_name)
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('sqlite:///mydatabase.db')

Session = sessionmaker(bind=engine)

Base = declarative_base(engine)

from .user import User

def create_db():
    Base.metadata.create_all()

以上是关于python sqlalchemy模型包的主要内容,如果未能解决你的问题,请参考以下文章

使用具有单个 sqlalchemy 模型的多个数据库

python python - sqlalchemy模型转换

Python 9 sqlalchemy ORM

Python之路51-sqlalchemy

python ORM框架:SqlAlchemy

Python开发:ORM sqlalchemy