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 6111139
In Process

The Archive Base Latest Questions

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

I’m using SQLAlchemy and I what I liked with Django ORM was the Manager

  • 0

I’m using SQLAlchemy and I what I liked with Django ORM was the Manager I could implement to override the initial query of an object.

Is something like this exist in SQLAlchemy? I’d like to always exclude items that have “visible = False”, when I do something like :

session.query(BlogPost).all()

Is it possible?

Thanks!

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

    EDIT: the original version almost worked. The following version actually works.

    It sounds like what you’re trying to do is arrange for the query entity to be something other than SELECT table.* FROM table. In sqlalchemy, you can map any “selectable” to a class; There are some caveats, though; if the selectable is not a table, inserting data can be tricky. Something like this approaches a workable solution. You probably do want to have a regular table mapped to permit inserts, so the first part is a totally normal table, class and mapper.

    blog_post_table = Table("blog_posts", metadata,
        Column('id', Integer, primary_key=True),
        Column('visible', Boolean, default=True),
        ...
    )
    
    class BlogPost(object):
        pass
    
    blog_post_mapper = mapper(BlogPost, blog_post_table)
    

    Or, if you were using the declarative extension, it’ll all be one

    class BlogPost(Base):
        __tablename__ = 'blog_posts'
        id = Column(Integer, primary_key=True)
        visible = Column(Boolean, default=True)
    

    Now, we need a select expression to represent the visible posts.

    visible_blog_posts_expr = sqlalchemy.sql.select(
            [BlogPost.id,
             BlogPost.visible]) \
        .where(BlogPost.visible == True) \
        .alias()
    

    Or, since naming all of the columns of the desired query is tedious (not to mention in violation of DRY), you can use the same construct as session.query(BlogPost) and extract the ‘statement’. You don’t actually want it bound to a session, though, so call the class directly.

    visible_blog_posts_expr = \
        sqlalchemy.orm.Query(BlogPost) \
        .filter(BlogPost.visible == True) \
        .statement \
        .alias()
    

    And we map that too.

    visible_blog_posts = mapper(BlogPost, visible_blog_posts_expr, non_primary=True)
    

    You can then use the visible_blog_posts mapper instead of BlogPosts with Session.query, and you will still get BlogPost, which can be updated and saved as normal.

    posts = session.query(visible_blog_posts).all()
    assert all(post.visible for post in posts)
    

    For this particular example, there’s not much difference between explicit mapper use and declarative extension, you still must call mapper for the non-primary mappings. At best, it allows you to type SomeClass.colname instead of some_table.c.colname (or SomeClass.__table__.colname, or BlogPost.metadata.tables[BlogPost.__tablename__] or … and so on).

    The mistakes I made in the original example, which are now corrected. I was missing some missing []‘s in the call to sqlalchemy.sql.select, which expects the columns to be in a sequence. when using a select statement to mapper, sqlalchemy insists that the statement be aliased, so that it can be named (SELECT .... ) AS some_subselect_alias_5

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i got an object with contents of html markup in it, for example: string

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.