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

  • SEARCH
  • Home
  • 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 6113045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:48:50+00:00 2026-05-23T14:48:50+00:00

I changed my Django application to use SQLAlchemy, and it works now. But I’m

  • 0

I changed my Django application to use SQLAlchemy, and it works now.

But I’m wondering where I should put these lines:

engine = sqlalchemy.create_engine(settings.DATABASE_URL)
Session = sqlalchemy.orm.sessionmaker(bind=engine)
session = Session()

The reason I’m asking is because I want to use SQLAlchemy at many place, and I don’t think its correct/powerful/well-written to call this three lines everytime I need to use the database.

The place I will require SA is :

  • In my views, of course
  • In some middleware I wrote
  • In my models. Like in get_all_tags for a BlogPost Model.

What I think would be correct, is to get the session, by re-connecting to the database if the session is closed, or just returning the current, connected session if exists.

How can I use SQLAlchemy correctly with my Django apps?

Thanks for your help!

Note: I already followed this tutorial to implement SA into my Django application, but this one doesn’t tell me exactly where to put those 3 lines (http://lethain.com/entry/2008/jul/23/replacing-django-s-orm-with-sqlalchemy/).

  • 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-23T14:48:51+00:00Added an answer on May 23, 2026 at 2:48 pm

    for the first two, engine and Session, you can put them in settings.py; they are, configuration, after all.

    Actually creating a session requires slightly more care, since a session is essentially a ‘transaction’. The simplest thing to do is to create it in each view function when needed, and commit them just before returning. If you’d like a little bit more magic than that, or if you want/need to use the session outside of the view function, you should instead define some middleware, something like

    class MySQLAlchemySessionMiddleware(object):
        def process_request(self, request):
            request.db_session = settings.Session()
    
        def process_response(self, request, response):
            try:
                session = request.db_session
            except AttributeError:
                return response
            try:
                session.commit()
                return response
            except:
                session.rollback()
                raise
    
        def process_exception(self, request, exception):
            try:
                session = request.db_session
            except AttributeError:
                return
            session.rollback()
    

    Then, every view will have a db_session attribute in their requests, which they can use as they see fit, and anything that was added will get commited when the response is finished.

    Don’t forget to add the middleware to MIDDLEWARE_CLASSES

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

Sidebar

Related Questions

I am writing a Django application, and I often use the Python backtick operator
I am hosting my Django application on Dreamhost. Dreamhost serves Django-1.2.1 on Python-2.5.2, but
I have a Django application, and I recently changed the name of the database
I am using Memcache for my Django application. In Django, developers can use template
I have a simple blog application written in Python, using Django. I use Git
Hi i try to do that example http://openlayers.org/dev/examples/all-overlays.html in my django application. I use
I'm migrating a Google App Engine application from Django 0.96 to 1.2 . In
I have a django model in use on a production application and I need
I've deployed a Django application on Heroku. The application by itself works fine. I
I intend to localise my Django application and began reading up on localisation on

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.