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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:16:39+00:00 2026-05-22T22:16:39+00:00

I want to get the value of a parameter in a sqlalchemy query object

  • 0

I want to get the value of a parameter in a sqlalchemy query object dynamically:

q = session.query(Model).filter(Model.foo = 6)

I now want to be able to retrieve the value 6 from q

assert(q.magic == 6)

Attempts:

print(q._criterion) # -> models.foo = :foo_1

But where is the value of foo_1?

  • 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-22T22:16:39+00:00Added an answer on May 22, 2026 at 10:16 pm

    SQLAlchemy generates a tree structure from your filter predicates, appending each leaf on as appropriate and putting the result in Query._criterion. You can explore this with the get_children() method of various ClauseElement and ColumnElement classes.

    For Model.foo == 6 you’ll end up with something like this:

                        Model.foo == 6        
                               |                 
                       _BinaryExpression
                              / \
                             /   \
                            /     \   
    Column('foo', Integer(),      _BindParamClause(u'%(168261004 foo)s',
                  ...)                             6, type_=Integer())  
    

    If you were to & together two predicates, like (Model.foo == 6) & (Model.name == 'a name') or by chaining filter calls, you’d get a BooleanClauseList with two _BinaryExpression children. This means you can’t hard code a simple expression to reliably return the values you want but instead have to traverse the predicate tree.

    The traverse function from sqlalchemy.sql.visitors does just that, relying on a dictionary of special names that relate each Element’s __visit_name__ attribute to a processing function. This is a breadth-first traversal, which as you can see is appropriate for the example below; there’s also a depth-first version available.

    The following function shows how to generate a list of column-parameter pairs from a given query. I adapted it from the Beaker caching example:

     def extract_cols_params(query):
          if query._criterion is None:
               return []
    
          c, v = [], []
    
          def visit_bindparam(bind):
               value = query._params.get(bind.key, bind.value)
               if callable(value):
                    value = value()
               v.append(value)
    
          def visit_column(col):
               c.append('%s.%s' % (col.table.name, col.name))
    
          visitors.traverse(query._criterion, # our predicate tree  
                            {}, # kwargs for the iterator used by
                                # the traversal; undeeded.
                            {'bindparam': visit_bindparam, # for _BindParamClauses
                             'column'   : visit_column})   # for Columns
          return zip(c, v)
    
    >>> extract_cols_params(Session.query(Model).filter((Model.foo == 6)
        ).filter(Model.name == 'a name'))
    [('models.foo', 6), ('models.name', 'a name')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the ASCII value of characters in a string in C#.
I want to get the MD5 Hash of a string value in SQL Server
Table: UserId, Value, Date. I want to get the UserId, Value for the max(Date)
I want to use the DateTime.TryParse method to get the datetime value of a
I want to serialize my enum-value as an int, but i only get the
I want to get the parameter in my table (HTLML) table every time the
Is it possible for an rspec method to get the value of the parameter
I want to get a value from an item in a DataRow and I
String text = ref=\ar\ alias=\as sa\ value=\asa\; Actually i want get the value of
I'm have the following URL : www.example.com?arg1=foobar I want to get the arg1 value.

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.