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

The Archive Base Latest Questions

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

is it possible to override the default type.DateTime behaviour when using sqlalchemy with raw

  • 0

is it possible to override the default type.DateTime behaviour when using sqlalchemy with raw sql expressions?

for example when using

myconnection.execute(text("select * from mytable where mydate > :mydate), {'mydate': mypythondatetimeobject)}

, i would like to have type.DateTime to automatically strip the TimeZone from the DateTime object.

UPDATE: i found out, that it works by using bindparam and a Typedecorator on the query like this:

class MyType(types.TypeDecorator):
    impl = types.DateTime

    def process_bind_param(self, value, dialect):
        if value is not None:
            return value.replace(tzinfo=None)

session.execute(
    text(myquery, bindparams=[bindparam('mydateparam', 
                                        type_=MyType())]), 
    {'mydateparam':mydate}
)

but i really dont want to use “bindparams” on every query. isnt it possible to simply replace or override the default types.DateTime behaviour somehow?

  • 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-16T14:50:26+00:00Added an answer on May 16, 2026 at 2:50 pm

    If you used SQL expressions based on Table metadata, then you’d just set your MyType on the appropriate columns. The plain text approach loses some convenience. You could stick your MyType into the sqlalchemy.types.type_map keyed to the datetime class, though this is currently not public API.

    If it were me I’d do:

    def mytext(stmt, params):
        bindparams = []
        for k, v in params:
            if isinstance(v, datetime):
                type_ = MyType()
            else:
                type_ = None
            bindparams.append(bindparam(k, v, type_=type))
        return text(stmt, bindparams=bindparams)
    

    then just use mytext(“select * from table where date=:date”, {‘date’:datetime.today()})

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

Sidebar

Related Questions

Is it possible to override the default WCF DataContractSerializer behaviour when Serialize/DeSerialize entities and
Is it possible to override the display property with its default value? For example
Is it possible to override system property (timezone etc.) in Java using predefined properties
I was wondering if anyone knew if it were possible to override the default
I'm trying to make an application where the user can override the default behaviour
Is it possible to set or override the default state for a structure? As
Is it possible to set or override the default state for a structure? As
Possible Duplicate: Override default php function Is there a way to detect that a
Is it possible to override default value of WebClientProtocol.Timeout property via web.config? <httpRuntime executionTimeout=500
Is it possible to override the default minlength error message in the callback invalidHandler

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.