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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:51:32+00:00 2026-06-04T03:51:32+00:00

With SQLAlchemy, it is possible to add a default value to every function. As

  • 0

With SQLAlchemy, it is possible to add a default value to every function. As I understand it, this may also be a callable (either without any arguments or with an optional ExecutionContext argument).

Now in a declarative scenario, I wonder if it is somehow possible to have a default function which is called with the object that is being stored. I.e. possibly like so:

Base = sqlalchemy.ext.declarative.declarative_base()
class BaseEntity(Base):
    value = Column('value', String(40), default=BaseEntity.gen_default)

    def gen_default(self):
        # do something with self, for example
        # generate a default value using some other data
        # attached to the object
        return self.default_value

Is something like this possible? Or do I have to somehow set up an before-insertion hook for this (how?)?

  • 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-06-04T03:51:34+00:00Added an answer on June 4, 2026 at 3:51 am

    before_insert is documented here:

    http://docs.sqlalchemy.org/en/rel_0_7/orm/events.html#sqlalchemy.orm.events.MapperEvents.before_insert

    examples here:

    http://docs.sqlalchemy.org/en/rel_0_7/orm/events.html#mapper-events

    i.e.

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import event
    
    Base= declarative_base()
    
    class A(Base):
        __tablename__ = "a"
    
    
        id = Column(Integer, primary_key=True)
        data = Column(String)
        otherdata = Column(String)
    
    @event.listens_for(A, "before_insert")
    def gen_default(mapper, connection, instance):
        instance.data = "Some default %s" % instance.otherdata
    
    e = create_engine("sqlite://")
    Base.metadata.create_all(e)
    
    a = A(otherdata="some other data")
    s = Session(e)
    s.add(a)
    s.commit()
    
    assert a.data == "Some default some other data"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible in SQLAlchemy to enforce maximum string length of value assigned to
is it possible to override the default type.DateTime behaviour when using sqlalchemy with raw
I'm reading about SQLAlchemy's connection pooling, which has a default of 5 connections and
Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
Is it possible to write custom collation functions with indexes in SQLAlchemy? SQLite for
In sqlalchemy 0.5 i have a table defined like this one: orders = Table('orders',
Is it possible to have multi-level polymorphism in SQLAlchemy? Here's an example: class Entity(Base):
Is it possible to have the postgres database dump(pg_dump) using SQLAlchemy? i can get
Using SQLAlchemy, an Engine object is created like this: from sqlalchemy import create_engine engine
this is my Flask-SQLAlchemy Declarative code: from sqlalchemy.ext.associationproxy import association_proxy from my_flask_project import db

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.