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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:55:59+00:00 2026-06-16T12:55:59+00:00

I want to create a custom interface on top of SQLAlchemy so that some

  • 0

I want to create a custom interface on top of SQLAlchemy so that some pre-defined hybrid properties are supported transparently.

Specifically, I want to create a class SpecialColumn and a metaclass so that when a user adds SpecialColumn as an attribute of a class, my custom metaclass replaces that attribute with two SQLAlchemy Columns and adds a hybrid property that gets and sets those two columns as a tuple.
Here’s my approach so far:

First, I defined my special column type:

class SpecialColumn(object):
     pass

Then, I defined a metaclass inheriting from DeclarativeMeta which scans the class for instances of SpecialColumn and replaces them with two Columns and a hybrid property (defined as a closure):

class MyDeclarativeMeta(DeclarativeMeta):

     def __new__(cls, name, bases, attrs):
          for name, col in attrs.items():
              if isinstance(col, SpecialColumn):
                  # Replacing the column
                  del attrs[name]
                  col1_name = '_{0}_1'.format(name)
                  col2_name = '_{0}_2'.format(name)
                  attrs[col1_name] = Column(...)
                  attrs[col2_name] = Column(...)
                  # Adding the hybrid property
                  def getter(self):
                      return (getattr(self, col1_name), getattr(self, col2_name))
                  attrs[name] = hybrid_property(getter)

And finally I constructed an instance of declarative_base with it, and let the user define classes with the new base:

MyBase = declarative_base(metaclass=MyDeclarativeMeta)

class MyClass(MyBase):
    col1 = SpecialColumn()
    col2 = Column(...)

Now for my questions:
Firstly, is my approach correct?
Secondly, how can I use the metaclass to add the setter? Would it be correct to do:

def setter(self, (v1, v2)):
    setattr(self, col1_name, v1)
    setattr(self, col2_name, v2)

And then simply do attrs[name].setter(setter) ?

  • 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-16T12:56:00+00:00Added an answer on June 16, 2026 at 12:56 pm

    There’s no need to use metaclasses for a SQLAlchemy mapped class as we supply plenty of events to add features to classes as they are created and/or mapped. mapper_configured might be good here, which if you’re on 0.8 you can apply to MyBase directly:

    @event.listens_for(MyBase, 'mapper_configured')
    def get_special_columns(mapper, cls):
        for attrname in dir(cls):
            val = getattr(cls, attrname)
            if isinstance(val, SpecialColumn):
                 name1, name2 = "_%s_1" % attrname, "_%s_2" % attrname
                 setattr(cls, name1, Column(...))
                 setattr(cls, name2, Column(...))
    
                 @hybrid_property
                 def myhybrid(self):
                     return getattr(self, name1), getattr(self, name2)
    
                 @myhybrid.setter
                 def myhybrid(self, value):
                     setattr(self, name1, value[0])
                     setattr(self, name2, value[1])
    
                 setattr(cls, attrname, myhybrid)
    

    note that setattr() is the best way to go here, simple and to the point.

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

Sidebar

Related Questions

I want to create a custom list in Flex for an interface prototype. The
i want create a custom json data from the mssql 2008 results so that
I want to create a custom set that will automatically convert objects into a
I want to create a custom style for an activity that will look like
I want to create a custom tab bar for an iphone app that looks
I want to create a 'hidden custom cell' effect like that used in the
I want to create custom button and I need it to be circle. How
I want to create a custom navigation bar to put in my header.phtml file.
I want to create a custom property on one of my entities mapped from
I want to create a custom alert view within my iOS application. For example,

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.