title: how can i change this SQL to Django ORM code?
body: <pre class="lang-sql prettyprint-override"><code>select *
  from sample
  join process
  on sample.processid = process.id
  where (processid) in (
      select max(processid) as processid
      from main_sample
      group by serialnumber
  ) 
  ORDER BY sample.create_at desc;
  </code></pre>
  <p>models.py</p>
  <pre class="lang-py prettyprint-override"><code>class Sample(models.Model):
      processid = models.IntegerField(default=0) 
      serialnumber = models.CharField(max_length=256)  ##
      create_at = models.DateTimeField(null=True)
  
  class Process(models.Model):
      sample = models.ForeignKey(Sample, blank=False, null=True, on_delete=models.SET_NULL)
  
  </code></pre>
  <p>Hi I have two models and I need to change this SQL query to Django ORM, Python code.</p>
  <p>I need to <strong>retrieve the latest Sample(by processid) per unique serial number</strong>.</p>
  <p><strong>for example,</strong></p>
  <p><a href="https://i.stack.imgur.com/UfRa1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UfRa1.png" alt="enter image description here" /></a></p>
  <p>=&gt; after RUN query</p>
  <p><a href="https://i.stack.imgur.com/AqoCx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AqoCx.png" alt="enter image description here" /></a></p>
  <p>How can I change the SQL query to ORM code?
  how can i change the subquery to ORM?</p>
  <p>Thanks for reading.</p>