2

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 )
......

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):
.......

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.

asked Sep 17, 2015 at 20:15
3

2 Answers 2

2

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

answered May 23, 2019 at 15:16
Sign up to request clarification or add additional context in comments.

1 Comment

It would be helpful to mention what specifically db.Model does that makes Flask integration easier.
-1

You can to create a module with the SQLAlchemy instance and use only the inheritance in the another's modules.

in database.py module:

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

movieModel.py

from database import db
class MovieModel(db.Model):
 __tablename__ = 'movie'
 id = db.Column(db.Integer, primary_key=True)

imageModel.py

from database import db
class ImageModel(db.Model):
 __tablename__ = 'image'
 id = db.Column(db.Integer, primary_key=True)
answered May 23, 2019 at 15:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.