How can I save my model in another file? #1065
-
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:[email protected]:5432/faceDetection'
app.config["SQLALCHEMY_ECHO"] = True
db = SQLAlchemy(app)
class ImageFileModel(db.Model):
id = db.Column(db.Integer, primary_key=True, unique=True,autoincrement=False)
#id = db.Column(db.Integer)
type = db.Column(db.Integer)
md5 = db.Column(db.String, index=True)
sha1 = db.Column(db.String, index=True)
faces_count = db.Column(db.Integer)
JsonResponse = db.Column(db.String)
error = db.Column(db.String)
currentFace = db.Column(db.Integer)
filepath = db.Column(db.String)
description = db.Column(db.String)
def __init__(self, id, type = 2, md5 ="", sha1="",faces_count=0,JsonResponse="",error="",currentFace=0, filepath="",description=""):
self.id = id
self.type = type
self.md5 = md5
self.sha1 = sha1
self.faces_count = faces_count
self.JsonResponse = JsonResponse
self.error = error
self.currentFace = currentFace
self.filepath = filepath
self.description = description How Can I save model in another file? Folders? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
that's more a generic python question about how to use modules/packages than flask/sqlalchemy-related... ps: don't add a constructor to your models. the built-in one does everything yours does, just much better. and default values belong in the column definition, not the constructor. |
Beta Was this translation helpful? Give feedback.
-
If your project looks like
Then as long as you import the models after defining the # __init__.py
app = Flask(__name__)
db = SQLAlchemy(app)
from . import models # models.py
from . import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True) You can also use the app factory pattern to prevent causing circular import issues. # __init__.py
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
db.init_app(app)
from . import models
return app |
Beta Was this translation helpful? Give feedback.
If your project looks like
Then as long as you import the models after defining the
db
object, they will be registered with SQLAlchemy.You can also use the app factory pattern to prevent causing circular import issues.