zoukankan      html  css  js  c++  java
  • Flask从数据库已有表自动生成model

    准备

    我们需要提前准备两个包,分别是 flask-sqlacodegen 和 psycopg2 安装方式为

    pip install flask-sqlacodegen
    pip install psycopg2

    开始

    我们直接使用命令开始生成

    # 单张表
    flask-sqlacodegen "postgresql://postgres:36o%*********2022@127.0.0.1:5432/situation" --tables TABLENAME --outfile "model.py" --flask
    # 多张表
    flask-sqlacodegen "postgresql://postgres:36o%*********2022@127.0.0.1:5432/situation" --tables TABLENAME,TABLENAME,TABLENAME --outfile "model.py" --flask
    # 全部表
    flask-sqlacodegen "postgresql://postgres:36o%*********2022@127.0.0.1:5432/situation" --outfile "model.py" --flask

    ps:(大写 “TABLENAME” 要换成自己对应的实际情况)

    此步骤有可能会报错,但是没关系,在目录下已经生成了对应文件

     最后

    我们最后在加上 inspect 完成初始化就OK啦!

    def __init__(self, **kwargs):
        for c in inspect(self).mapper.column_attrs:
            setattr(self, c.key, kwargs.get(c.key)) if kwargs.get(c.key) else setattr(self, c.key, None)
    
    def to_dict(self):
        return {c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs}
  • 相关阅读:
    吃货联盟订餐系统
    第一章课后习题
    hostapd阅读(openwrt)-1
    通过C语言获取MAC地址(转)
    OpenWrt 编译分割
    ubuntu 12.04无盘工作站
    WEB前端性能优化-如何提高页面加载速度
    HTML6
    easyui 根据值查找匹配
    收藏的网站
  • 原文地址:https://www.cnblogs.com/shangwei/p/15787179.html
Copyright © 2011-2022 走看看