title: Retrieving most recent records from a table via fields automatically
body: <p>Sorry for the unclear title, I really had no idea what to put in there which will describe my problem exactly, so you're welcome to edit my question.</p>
  
  <p>I'm building my first Django webapp, which is supposed to be an application to manage payrolls for employees (this is going to be something internal for my work place). I do this mostly to learn Django, but also to build something useful.</p>
  
  <p>My question is about saving some historic data, but retrieving always only the current most recent data for each employee. I'll provide an example, and hopefully it will be easier for me to explain.</p>
  
  <p>Let's say we have 2 models (and I really do have a similar implementation of those models):</p>
  
  <pre><code>class Employee(models.Model):
      id = models.IntegerField(primary_key=True)
      first_name = models.CharField(max_length=50)
      last_name = models.CharField(max_length=50)
  
      address = &lt;not implemented yet&gt;
  
  
  class EmployeeAddress(models.Model):
      id = models.IntegerField(primary_key=True)
      employee = models.ForeignKey('Employee')
      city = models.CharField(max_length=50)
      street = models.CharField(max_length=50)
      house = models.IntegerField()
      start_date = models.DateField()
  </code></pre>
  
  <p>I would like that when I retrieve the field <code>address</code> from an <code>Employee</code> instance, I will get only the most recent address of that employee. Note that the <code>EmployeeAddress</code> table saves all address changes for each employee. In most cases I only need the most recent address, and I would like to retrieve it from the employee instance directly. Now, I could solve this by setting the <code>address</code> field to be:</p>
  
  <pre><code> address = models.OneToOneField('EmployeeAddress')
  </code></pre>
  
  <p>and make sure I update it to point to the most recent record in the <code>EmployeeAddress</code> table each time add a new address.</p>
  
  <p>But a more interesting case is shown in the following example:</p>
  
  <p>Say I have another model:</p>
  
  <pre><code>class EmployeePhone(models.Model):
      id = models.IntegerField(primary_key=True)
      employee = models.ForeignKey('Employee')
      number = models.IntegerField()
      start_date = models.DateField()
      end_date = models.DateField()
  </code></pre>
  
  <p>Now, in this case, each employee may have several (active) phone records (these might be home phone, work phone, cellular phone, etc...).
  In this case I would like to add a new field to <code>Employee</code> which will be called: <code>phone_number</code>, and make sure that whenever this field is accessed I will get a list of only the active phone records for that employee. Instead of querying explicitly the <code>EmployeePhone</code> backwards from <code>Employee</code> and then filtering by the records which have a missing <code>end_date</code> each time I want to access the phone numbers, I would like an easy access to those records from the field of the employee itself.</p>
  
  <p>If the phone numbers tables would not have saved history (meaning all records were active, and once a record is not longer valid, we just remove it), It was very simple, I could get all the phone records by iterating over <code>Employee.employeephone_set</code> - these were all the valid phone numbers for that employee. But since we have history, this is no longer valid option for me, and I need to add additional query (filter) on <code>Employee.employeephone_set</code> to retrieve only the most recent active phone numbers.</p>
  
  <p>There are many more examples, like bank accounts history table (similar to the address example), and office titles for each employee (which could be several for each employee), and many more.</p>
  
  <p>Now, there is a way I could think of, to solve my problem and that is to implement a property in Employee that queries and filters the phone table automatically whenever the get method for that property is invoked. But I would like to hear other solutions to my (not so hard) problem.</p>
  
  <p>I probably made the problem look like a big deal by this long post, but actually it isn't.. I'm just thinking maybe I'm missing something.. maybe Django actually supports what I want to accomplish somehow.</p>