我使用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模型
发布评论