title: How to get dict of lists from relationship in sqlalchemy?
body: <p>I've found out that you can use collections in relationship in order to change the type of return value, specifically I was interested in dictionaries.
  Documentation gives an <a href="https://docs.sqlalchemy.org/en/13/orm/collections.html#customizing-collection-access" rel="nofollow noreferrer">example</a>:</p>
  <pre><code>class Item(Base):
      __tablename__ = 'item'
      id = Column(Integer, primary_key=True)
      notes = relationship(&quot;Note&quot;,
                           collection_class=attribute_mapped_collection('keyword'),
                           cascade=&quot;all, delete-orphan&quot;)
  
  class Note(Base):
      __tablename__ = 'note'
      id = Column(Integer, primary_key=True)
      item_id = Column(Integer, ForeignKey('item.id'), nullable=False)
      keyword = Column(String)
      text = Column(String)
  </code></pre>
  <p>And it works. However I was hoping that it will make list values if there are more than just one key with the same name. But it only puts the last value under unique key name.</p>
  <p>Here is an example:</p>
  <pre><code>|               Note table               |
  |---------------------|------------------|
  |          id         |      keyword     |
  |---------------------|------------------|
  |          1          |        foo       |
  |---------------------|------------------|
  |          2          |        foo       |
  |---------------------|------------------|
  |          3          |        bar       |
  |---------------------|------------------|
  |          4          |        bar       |
  |---------------------|------------------|
  </code></pre>
  <p><code>item.notes</code> will return something like this:</p>
  <pre><code>{'foo': &lt;project.models.note.Note at 0x7fc6840fadd2&gt;,
   'bar': &lt;project.models.note.Note at 0x7fc6840fadd4&gt;}
  </code></pre>
  <p>Where ids of foo and bar objects are 2 and 4 respectively.</p>
  <p>What I'm looking for is to get something like this:</p>
  <pre><code>{'foo': [&lt;project.models.note.Note at 0x7fc6840fadd1,
            &lt;project.models.note.Note at 0x7fc6840fadd2&gt;],
   'bar': [&lt;project.models.note.Note at 0x7fc6840fadd3&gt;,
           &lt;project.models.note.Note at 0x7fc6840fadd4&gt;]}
  </code></pre>
  <p>Is it possible to get dict of lists from relationship in sqlalchemy?</p>