title: pydantic: Using property.getter decorator for a field with an alias
body: <p><em>scroll all the way down for a tl;dr, I provide context which I think is important but is not directly relevant to the question asked</em></p>
  <h2>A bit of context</h2>
  <p>I'm in the making of an API for a webapp and some values are computed based on the values of others in a <em>pydantic</em> <code>BaseModel</code>. These are used for user validation, data serialization and definition of database (NoSQL) documents.</p>
  <p>Specifically, I have nearly all resources inheriting from a <code>OwnedResource</code> class, which defines, amongst irrelevant other properties like creation/last-update dates:</p>
  <ul>
  <li><code>object_key</code> -- The key of the object using a <a href="https://pypi.org/project/nanoid/" rel="noreferrer">nanoid</a> of length 6 with a custom alphabet</li>
  <li><code>owner_key</code> -- This key references the user that owns that object -- a nanoid of length 10.</li>
  <li><code>_key</code> -- this one is where I'm bumping into some problems, and I'll explain why.</li>
  </ul>
  <p>So arangodb -- the database I'm using -- imposes <code>_key</code> as the name of the property by which resources are identified.</p>
  <p>Since, in my webapp, all resources are only accessed by the users who created them, they can be identified in URLs with just the object's key (eg. <code>/subject/{object_key}</code>). However, as <code>_key</code> must be unique, I intend to construct the value of this field using <code>f&quot;{owner_key}/{object_key}&quot;</code>, to store the objects of every user in the database and potentially allow for cross-user resource sharing in the future.</p>
  <p>The goal is to have the shortest <strong>per-user</strong> unique identifier, since the <code>owner_key</code> part of the full <code>_key</code> used to actually access and act upon the document stored in the database is always the same: the currently-logged-in user's <code>_key</code>.</p>
  <h2>My attempt</h2>
  <p>My thought was then to define the <code>_key</code> field as a <code>@property</code>-decorated function in the class. However, Pydantic does not seem to register those as model fields.</p>
  <p>Moreover, the attribute must actually be named <code>key</code> and use an alias (with <code>Field(... alias=&quot;_key&quot;</code>), as <a href="https://github.com/samuelcolvin/pydantic/issues/288" rel="noreferrer">pydantic treats underscore-prefixed fields as internal and does not expose them</a>.</p>
  <p>Here is the definition of <code>OwnedResource</code>:</p>
  <pre class="lang-py prettyprint-override"><code>class OwnedResource(BaseModel):
      &quot;&quot;&quot;
      Base model for resources owned by users
      &quot;&quot;&quot;
  
      object_key: ObjectBareKey = nanoid.generate(ID_CHARSET, OBJECT_KEY_LEN)
      owner_key: UserKey
      updated_at: Optional[datetime] = None
      created_at: datetime = datetime.now()
  
      @property
      def key(self) -&gt; ObjectKey:
          return objectkey(self.owner_key)
  
      class Config:
          fields = {&quot;key&quot;: &quot;_key&quot;} # [1]
  </code></pre>
  <p>[1] Since Field(..., alias=&quot;...&quot;) cannot be used, I use this property of the Config subclass (see <a href="https://pydantic-docs.helpmanual.io/usage/model_config/" rel="noreferrer">pydantic's documentation</a>)</p>
  <p>However, this does not work, as shown in the following example:</p>
  <pre class="lang-py prettyprint-override"><code>@router.post(&quot;/subjects/&quot;)
  def create_a_subject(subject: InSubject):
      print(subject.dict(by_alias=True))
  </code></pre>
  <p>with <code>InSubject</code> defining properties proper to <code>Subject</code>, and <code>Subject</code> being an empty class inheriting from both <code>InSubject</code> and <code>OwnedResource</code>:</p>
  <pre class="lang-py prettyprint-override"><code>class InSubject(BaseModel):
      name: str
      color: Color
      weight: Union[PositiveFloat, Literal[0]] = 1.0
      goal: Primantissa # This is just a float constrained in a [0, 1] range
      room: str
  
  class Subject(InSubject, OwnedResource):
      pass
  </code></pre>
  <p>When I perform a <code>POST /subjects/</code>, the following is printed in the console:</p>
  <pre class="lang-py prettyprint-override"><code>{'name': 'string', 'color': Color('cyan', rgb=(0, 255, 255)), 'weight': 0, 'goal': 0.0, 'room': 'string'}
  </code></pre>
  <p>As you can see, <code>_key</code> or <code>key</code> are nowhere to be seen.</p>
  <p>Please ask for details and clarification, I tried to make this as easy to understand as possible, but I'm not sure if this is clear enough.</p>
  <h2>tl;dr</h2>
  <p>A context-less and more generic example without insightful context:</p>
  <p>With the following class:</p>
  <pre class="lang-py prettyprint-override"><code>from pydantic import BaseModel
  
  class SomeClass(BaseModel):
      
      spam: str
  
      @property
      def eggs(self) -&gt; str:
          return self.spam + &quot; bacon&quot;
  
      class Config:
          fields = {&quot;eggs&quot;: &quot;_eggs&quot;}
  </code></pre>
  <p>I would like the following to be true:</p>
  <pre class="lang-py prettyprint-override"><code>a = SomeClass(spam=&quot;I like&quot;)
  d = a.dict(by_alias=True)
  d.get(&quot;_eggs&quot;) == &quot;I like bacon&quot;
  </code></pre>