Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:36:58+00:00 2026-05-31T12:36:58+00:00

I want to structure my Flask app something like: ./site.py ./apps/members/__init__.py ./apps/members/models.py apps.members is

  • 0

I want to structure my Flask app something like:

./site.py
./apps/members/__init__.py
./apps/members/models.py

apps.members is a Flask Blueprint.

Now, in order to create the model classes I need to have a hold of the app, something like:

# apps.members.models
from flask import current_app
from flaskext.sqlalchemy import SQLAlchemy

db = SQLAlchemy(current_app)

class Member(db.Model):
    # fields here
    pass

But if I try and import that model into my Blueprint app, I get the dreaded RuntimeError: working outside of request context. How can I get a hold of my app correctly here? Relative imports might work but they’re pretty ugly and have their own context issues, e.g:

from ...site import app

# ValueError: Attempted relative import beyond toplevel package
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:36:59+00:00Added an answer on May 31, 2026 at 12:36 pm

    The flask_sqlalchemy module does not have to be initialized with the app right away – you can do this instead:

    # apps.members.models
    from flask_sqlalchemy import SQLAlchemy
    
    db = SQLAlchemy()
    
    class Member(db.Model):
        # fields here
        pass
    

    And then in your application setup you can call init_app:

    # apps.application.py
    from flask import Flask
    from apps.members.models import db
    
    app = Flask(__name__)
    # later on
    db.init_app(app)
    

    This way you can avoid cyclical imports.

    This pattern does not necessitate the you place all of your models in one file. Simply import the db variable into each of your model modules.

    Example

    # apps.shared.models
    from flask_sqlalchemy import SQLAlchemy
    
    db = SQLAlchemy()
    

    # apps.members.models
    from apps.shared.models import db
    
    class Member(db.Model):
        # TODO: Implement this.
        pass
    

    # apps.reporting.members
    from flask import render_template
    from apps.members.models import Member
    
    def report_on_members():
        # TODO: Actually use arguments
        members = Member.filter(1==1).all()
        return render_template("report.html", members=members)
    

    # apps.reporting.routes
    from flask import Blueprint
    from apps.reporting.members import report_on_members
    
    reporting = Blueprint("reporting", __name__)
    
    reporting.route("/member-report", methods=["GET","POST"])(report_on_members)
    

    # apps.application
    from flask import Flask
    from apps.shared import db
    from apps.reporting.routes import reporting
    
    app = Flask(__name__)
    db.init_app(app)
    app.register_blueprint(reporting)
    

    Note: this is a sketch of some of the power this gives you – there is obviously quite a bit more that you can do to make development even easier (using a create_app pattern, auto-registering blueprints in certain folders, etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very complex web app project I want to re-structure. Naturally, it
I am new to svg and dont know its structure.I want to create 10
I want to create a structure Degrees for a GPX library. In the XSD
I want to create tree structure using web service. I have used bottom up
I want to create a table structure with checkbox for each row and each
I am trying to create something like a list. However, different instances of the
I have structure(A) and it takes another structure(B) as member. Now i want structure
I have structure(A) and it takes another structure(B) as member. Now i want structure
I have the following routes. I want the structure of the app to remain
I have a table Sample and another SampleLog With these structure I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.