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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:25:51+00:00 2026-06-12T15:25:51+00:00

I am attempting 2 new things at once, so assistance in both simplifying and

  • 0

I am attempting 2 new things at once, so assistance in both simplifying and clarifying is appreciated.

from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy import Column, Float, event

class TimeStampMixin(object):

    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()

    created = Column(Float)
    modified = Column(Float)
    def __init__(self, created = None,
                       modified = None):
        self.created = created
        self.modified = modified

def create_time(mapper, connection, target):
    target.created = time()

#def modified_time(mapper, connection, target):
#    target.modified = time()

event.listen(TimeStampMixin, 'before_insert', create_time)
#event.listen(TimeStampMixin, 'before_update', modified_time)

So I want to create a mixin I can apply in any class:

class MyClass(TimeStampMixin, Base):
    etc, etc, etc

This class inherits functionality that creates a timestamp on creation and creates/modifies a timestamp on update.

on import I get this error:

raise exc.UnmappedClassError(class_)
sqlalchemy.orm.exc.UnmappedClassError: Class 'db.database.TimeStampMixin' is not mapped

aaaand I’m stumped at this point.

  • 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-12T15:25:52+00:00Added an answer on June 12, 2026 at 3:25 pm

    Here’s what I’d do to listen on before_insert events: add a classmethod to your TimeStampMixin that registers the current class and handles setting creation time.

    E.g.

    class TimeStampMixin(object):
    
        # other class methods
    
        @staticmethod
        def create_time(mapper, connection, target):
            target.created = time()
    
        @classmethod
        def register(cls):
            sqlalchemy.event.listen(cls, 'before_insert', cls.create_time)
    

    That way, you can:

    1. Easily extend and change what you listen for and what you register.
    2. Override the create_time method for certain classes
    3. Be explicit about which methods need to have their timestamps set.

    You can use it simply:

    class MyMappedClass(TimeStampMixin, Base):
        pass
    
    MyMappedClass.register()
    

    Simple, very clear, no magic, but still encapsulates like you want.

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

Sidebar

Related Questions

I am new to all things maven and ignition. I am attempting to set
I am new to Ember and attempting to learn parts of it from the
Im attempting to create a new operator :? on lists, which operates the same
I am new to jQuery and attempting to use the dropdownchecklist control I am
I am New to Ada and I am attempting to convert the code in
I am attempting to use a new settings file in my Windows Forms Application
I am new to WSO2 ESB and am attempting to get a very simple
I'm attempting to use the new standard way of loading seed data in Rails
I am new to Linux and I am attempting to install the PHP PEAR
I am attempting to connect a new Orbeon XForm to an existing exist 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.