title: How can I organize SQLAlchemy relationships for access like a JSON-like dict?
body: <p>I have an application where each user can have many different clothing sizes associated with their account. I have modeled these associations with association tables, and linked them through <code>relationship()</code>s in my models. Before I began trying to shove the relationships into a dictionary, they were linked at the top level, like so:</p>
  
  <pre><code>class User(db.Model):
  
      id = db.Column(db.Integer, primary_key=True)
  
      # user many-to-many size associations (using link tables)
      sz_shirt_dress_sleeve = db.relationship(
          'SizeKeyShirtDressSleeve',
          secondary=LinkUserSizeShirtDressSleeve,
          backref=db.backref('users', lazy='dynamic'))
      sz_shirt_dress_neck = db.relationship(
          'SizeKeyShirtDressNeck',
          secondary=LinkUserSizeShirtDressNeck,
          backref=db.backref('users', lazy='dynamic'))
      sz_shirt_casual = db.relationship(
          'SizeKeyShirtCasual',
          secondary=LinkUserSizeShirtCasual,
          backref=db.backref('users', lazy='dynamic'))
  </code></pre>
  
  <p>And I could access them like so:</p>
  
  <pre><code>&gt;&gt;&gt; from app.models import User
  &gt;&gt;&gt; u1 = User.query.first()
  &gt;&gt;&gt; u1.sz_shirt_dress_sleeve
  [Dress shirt sleeve size: 3000]
  </code></pre>
  
  <p>Because I will eventually have ~20 of these relationships, and because I want to access them more programmatically elsewhere, I would like to store them in a JSON looking dict like this:</p>
  
  <pre><code>class User(db.Model):
  
      id = db.Column(db.Integer, primary_key=True)
  
      sizes = {
          'shirt-sleeve': {
              'key': 'shirt-sleeve',
              'values': db.relationship(
                  'SizeKeyShirtDressSleeve',
                  secondary=LinkUserSizeShirtDressSleeve,
                  backref=db.backref('users', lazy='dynamic'))
          }
      }
  </code></pre>
  
  <p>By doing that, I would be able to access the list of sizes like this:</p>
  
  <pre><code>&gt;&gt;&gt; &lt;some_user&gt;.sizes['shirt-sleeve']['values']
  [3000, 3050, 3100]
  </code></pre>
  
  <p>The above attempt at fitting these associations inside a dict doesn't work, as I've found, because <a href="https://stackoverflow.com/questions/50276826/when-accessed-similarly-why-does-a-sqlalchemy-relationship-return-differently/50277707#50277707">"[SQLAlchemy's use of metaclasses] doesn't pick up these objects inside something else like a dictionary."</a> Well crap. </p>
  
  <p>In a roundabout way to I tried to appease the metaclass top level sensing with this:</p>
  
  <pre><code>class User(db.Model):
  
      id = db.Column(db.Integer, primary_key=True)
  
      sz_shirt_dress_neck = db.relationship(
          'SizeKeyShirtDressNeck',
          secondary=LinkUserSizeShirtDressNeck,
          backref=db.backref('users', lazy='dynamic'))
  
      sizes = {
          'shirt-neck': {
              'key': 'shirt-neck',
              'values': lambda self: self.sz_shirt_dress_neck
          }
      }
  </code></pre>
  
  <p>That didn't work (though it didn't error either):</p>
  
  <pre><code>&gt;&gt;&gt; from app.models import User
  &gt;&gt;&gt; u1 = User.query.first()
  &gt;&gt;&gt; u1.sizes['shirt-neck']['values']
  &lt;function User.&lt;lambda&gt; at 0x102aedb70&gt;
  </code></pre>
  
  <p>How can I store these relationships in a JSON like dictionary? (and a small side question: where can I learn design principles to teach me if I <em>should</em> do it this way? I'm self taught and these larger design concepts aren't really covered by documentation/tutorials)</p>