title: SQLAlchemy - Expression for hybrid property
body: <p>I have a job and a task model where a job has several tasks, each of these tasks has a status. The Job is finished when the status of the latest task (by timestamp) has a status of <code>done</code>. All earlier statuses in the task list do not matter from the perspective of the job.</p>
  <pre><code>class Job(Base):
      __tablename__ = &quot;job&quot;
      id = Column(Integer, primary_key=True)
      tasks = relationship(&quot;Task&quot;, order_by=&quot;desc(Task.created)&quot;, back_populates=&quot;job&quot;)
  
  class Task(Base):
      __tablename__ = &quot;task&quot;
      id = Column(Integer, primary_key=True)
      status = Column(Integer, nullable=False)
      created = Column(DateTime, default=func.now())
  
      job_id = Column(Integer, ForeignKey(&quot;job.id&quot;), nullable=False)
      job = relationship(&quot;Job&quot;, back_populates=&quot;task&quot;)
  </code></pre>
  <p>I added a <code>hybrid property</code> to the Job model to give it its own status based on the status of the first task item:</p>
  <pre><code>@hybrid_property
  def status(self):
      if self.tasks:
          return self.tasks[0].status
      else:
          return None
  </code></pre>
  <p>This works when I query the model and look at the computed status:</p>
  <pre><code>&gt;&gt; job = session.query(Job).first()
  &gt;&gt; job.status  # 4 (pending)
  </code></pre>
  <p>When I reference it on the class level (<code>Job.status</code>) , however, it doesn't work. I get a <code>NotImplementedError: &lt;built-in function getitem&gt;</code>. I think that I also need to define the <code>@status.expression</code> on the Job model but I haven't managed to do that so far and I can't find any good examples, neither in the docs nor in other places. I'd be super grateful for any pointers here.</p>
  <p><em>Side note:</em> It would be even nicer to have the <code>Job.status</code> computed by taking the latest timestamp into account (instead of only grabbing the topmost item in the <code>Job.tasks</code> list. But I guess this is not strictly necessary since the tasks are sorted by timestamp with <code>order_by=&quot;desc(Task.created)&quot;</code>.</p>