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}
  • 相关阅读:
    03_ if 练习 _ little2big
    uva 11275 3D Triangles
    uva 12296 Pieces and Discs
    uvalive 3218 Find the Border
    uvalive 2797 Monster Trap
    uvalive 4992 Jungle Outpost
    uva 2218 Triathlon
    uvalive 3890 Most Distant Point from the Sea
    uvalive 4728 Squares
    uva 10256 The Great Divide
  • 原文地址:https://www.cnblogs.com/shangwei/p/15787179.html
Copyright © 2011-2022 走看看