Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Separation of Duties #244

Open
diloreto opened this issue Mar 19, 2022 · 1 comment
Open

Better Separation of Duties #244

diloreto opened this issue Mar 19, 2022 · 1 comment

Comments

@diloreto
Copy link

Hi Team - Love the repo and thank you. As you can imagine, once you begin to create larger and larger applications, the number of routes, models and schemas begin to increase, and become increasingly complex. Could you refactor your code to show how you would recommend separating your Marshmallow/SQLAlchemy Models from the main Flask app/routes? Right now you're instantiating the marshmallow object (ma) inside the main app.py file and then creating Marshmallow schemas below it. When you start having dozens or more tables/models this becomes unwieldy and increases the blast radius of app.py.

Thanks!

@sirosen
Copy link
Collaborator

sirosen commented Apr 13, 2023

I believe the best way to show this structure would be a dedicated schemas subpackage in your project which defines the flask-marshmallow extension object. Your app factory then imports that and calls init_app.

e.g.

# foo/schemas/__init__.py
from flask_marshmallow import Marshmallow

ma = Marshmallow()

# foo/app.py
from flask import Flask
from foo.schemas import ma

def create_app() -> Flask:
    app = Flask(__name__)
    ma.init_app(app)
    return app

I would happily look at a PR for this and help review/refine docs, so I've marked it help wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants