Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems that you can just use the attributes directly instead of using the <code>columns</code> <code>dict</code>.</p> <p>Consider the following setup:</p> <pre><code>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) </code></pre> <p>With that empty class you can do:</p> <pre><code>w = Word() w.english = u'name' w.korean = u'이름' w.romanian = u'nume' session.add(w) session.commit() </code></pre> <p>And when you want to access the data:</p> <pre><code>w = session.query(Word).filter_by(english=u'name').one() print w.romanian </code></pre> <p>That's the <strong>whole <code>sqlalchemy</code>'s ORM point</strong>, instead of using a <code>tuple</code> or <code>dict</code> to access the data, you use <em>attribute-like</em> access on your own class.</p> <p>So I was wondering for reasons you'd like to use a <code>dict</code>. Perhaps it's because you have strings with the language names. Well, for that you could use python's <code>getattr</code> and <code>setattr</code> instead, as you would on any python object:</p> <pre><code>language = 'korean' print getattr(w, language) </code></pre> <p>That should solve all of your issues.</p> <hr> <p>That said, if you still want to use <code>dict</code>-like access to the columns, it is also possible. You just have to implement a <code>dict</code>-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.</p> <p>You could do it on the <code>Word</code> class:</p> <pre><code>class Word(object): def __getitem__(self, item): return getattr(self, item) def __setitem__(self, item, value): return setattr(self, item, value) </code></pre> <p>The rest of the setup works as above. Then you could use it like this:</p> <pre><code>w = Word() w['english'] = u'name' </code></pre> <p>If you want a <code>columns</code> attribute then you need a <code>dict</code>-like </p> <pre><code>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) </code></pre> <p>Then you could use as you intended:</p> <pre><code>w = Word() w.columns['english'] = u'name' </code></pre> <p>I think you'll agree that all this is unnecessarly complicated with no added benefit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload