在烧瓶中使用SqlAlchemy模型

编程入门 行业动态 更新时间:2024-10-25 14:23:00
本文介绍了在烧瓶中使用SqlAlchemy模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用SqlAlchemy创建了一些模型,用于初始设置数据库.最初,我解析一些XML文件并填充数据库.当我在服务器上设置应用程序时,这是一件一次性的事情.

I created some models using SqlAlchemy for setting up the database initially. Initially i parse some XML files and populate the database. This is a one time thing which needs to be done when i setup the app on server.

Base = declarative_base() class Movie(Base): __tablename__ = 'Movies' id = Column(Integer, primary_key=True, autoincrement=True) title = Column(String(80)) filename = Column(String(80), unique=True) genre = Column(String(80)) language = Column(String(80)) year = Column(Integer) description = Column(Text) poster = Column(String) def __init__(self, title, filename, genre, language, year, description, poster): self.title = title self.filename = filename self.genre = genre self.language = language self.year = year self.description = description self.poster = poster def __repr__(self): return '<Movie (id=%d, title=%s, filename=%s, genre=%s, year=%s, description=%s, poster=%s)>' % ( self.id, self.title, self.filename, self.genre, self.year, self.description, self.poster ) ......

现在,我想对REST api使用烧瓶中的相同模型.但是从我所看到的来看,首先我需要使用flask应用程序创建数据库实例-像这样

Now i want to use the same models in the flask also for a REST api. But from what i have seen, first i need to create a db instance using the flask app - like this

app = Flask(__name__) db = SQLAlchemy(app) class Movie(db.Model): .......

此锻炼方式如何?由于我的模型是从Base类继承的,但是对于flask,它们需要从db.Model类继承.

How does this workout? Since my models are inheriting from the Base class but for flask they need to inherit from the db.Model class.

推荐答案

您可以按原样使用模型,而不必从" db.Model继承它们,这只是说服了使烧瓶集成更容易

you can simply use your models as they are, they do not "need" to inherit from db.Model, that is simply a convince to make flask integration easier

更多推荐

在烧瓶中使用SqlAlchemy模型

本文发布于:2023-10-13 10:34:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烧瓶   模型   SqlAlchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!