sqlalchemy dynamic mapping

前端 未结 2 533
攒了一身酷
攒了一身酷 2020-12-23 23:42

I have the following problem:

I have the class:


class Word(object):

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

    d         


        
相关标签:
2条回答
  • 2020-12-24 00:10

    I used nosklo's solution (thanks!) but I already had a primary key (passed in as pk_col) within the column line (first line of csv). So I thought I'd share my modification. I used a ternary.

    table = Table(tablename, metadata,
        *((Column(pk_col, Integer, primary_key=True)) if rowname == pk_col else (Column(rowname, String())) for rowname in row.keys()))
    table.create()
    
    0 讨论(0)
  • 2020-12-24 00:12

    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 讨论(0)
提交回复
热议问题