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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:11:39+00:00 2026-05-14T06:11:39+00:00

I have the following problem: I have the class: class Word(object): def __init__(self): self.id

  • 0

I have the following problem:

I have the class:


class Word(object):

    def __init__(self):
        self.id = None
        self.columns = {}

    def __str__(self):
        return "(%s, %s)" % (str(self.id), str(self.columns))

self.columns is a dict which will hold (columnName:columnValue) values. The name of the columns are known at runtime and they are loaded in a wordColumns list, for example


wordColumns = ['english', 'korean', 'romanian']

wordTable = Table('word', metadata,
                  Column('id', Integer, primary_key = True)
                  )
for columnName in wordColumns:
    wordTable.append_column(Column(columnName, String(255), nullable = False))

I even created a explicit mapper properties to “force” the table columns to be mapped on word.columns[columnName], instead of word.columnName, I don’t get any error on mapping, but it seems that doesn’t work.


mapperProperties = {}
for column in wordColumns:
    mapperProperties['columns[\'%']' % column] = wordTable.columns[column]

mapper(Word, wordTable, mapperProperties)

When I load a word object, SQLAlchemy creates an object which has the word.columns[‘english’], word.columns[‘korean’] etc. properties instead of loading them into word.columns dict. So for each column, it creates a new property. Moreover word.columns dictionary doesn’t even exists.

The same way, when I try to persist a word, SQLAlchemy expects to find the column values
in properties named like word.columns[‘english’] (string type) instead of the dictionary word.columns.

I have to say that my experience with Python and SQLAlchemy is quite limited, maybe it isn’t possible to do what I’m trying to do.

Any help appreciated,

Thanks in advance.

  • 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-14T06:11:39+00:00Added an answer on May 14, 2026 at 6:11 am

    It seems that you can just use the attributes directly instead of using the columns dict.

    Consider the following setup:

    from sqlalchemy import Table, Column, Integer, Unicode, MetaData, create_engine
    from sqlalchemy.orm import mapper, create_session
    
    class Word(object):
        pass
    
    wordColumns = ['english', 'korean', 'romanian']
    e = create_engine('sqlite://')
    metadata = MetaData(bind=e)
    
    t = Table('words', metadata, Column('id', Integer, primary_key=True),
        *(Column(wordCol, Unicode(255)) for wordCol in wordColumns))
    metadata.create_all()
    mapper(Word, t)
    session = create_session(bind=e, autocommit=False, autoflush=True)
    

    With that empty class you can do:

    w = Word()
    w.english = u'name'
    w.korean = u'이름'
    w.romanian = u'nume'
    
    session.add(w)
    session.commit()
    

    And when you want to access the data:

    w = session.query(Word).filter_by(english=u'name').one()
    print w.romanian
    

    That’s the whole sqlalchemy‘s ORM point, instead of using a tuple or dict to access the data, you use attribute-like access on your own class.

    So I was wondering for reasons you’d like to use a dict. Perhaps it’s because you have strings with the language names. Well, for that you could use python’s getattr and setattr instead, as you would on any python object:

    language = 'korean'
    print getattr(w, language)
    

    That should solve all of your issues.


    That said, if you still want to use dict-like access to the columns, it is also possible. You just have to implement a dict-like object. I will now provide code to do this, even though I think it’s absolutely unnecessary clutter, since attribute access is so clean. If your issue is already solved by using the method above, don’t use the code below this point.

    You could do it on the Word class:

    class Word(object):
        def __getitem__(self, item): 
            return getattr(self, item)
        def __setitem__(self, item, value):
            return setattr(self, item, value)
    

    The rest of the setup works as above. Then you could use it like this:

    w = Word()
    w['english'] = u'name'
    

    If you want a columns attribute then you need a dict-like

    class AttributeDict(DictMixin):
        def __init__(self, obj):
            self._obj = obj
        def __getitem__(self, item):
            return getattr(self._obj, item)
        def __setitem__(self, item, value):
            return setattr(self._obj, item, value)
    
    class Word(object):
        def __init__(self):
            self.columns = AttributeDict(self)
    

    Then you could use as you intended:

    w = Word()
    w.columns['english'] = u'name' 
    

    I think you’ll agree that all this is unnecessarly complicated with no added benefit.

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

Sidebar

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.