title: Django select_related with join on multiple fields
body: <p>I have a somewhat complex database in django. It makes extensive use of partitioned tables. I have had some problems before with django and partitioned tables but so far I have found satisfactory solutions to all of my problems.</p>
  
  <p>My newest Problem concerns a foreign key relationship between two partitioned tables. I have a model called Event with a submodel called MonitorEvent that houses some additional information  for this type of events. The, for my question, important fields of the models are:</p>
  
  <pre><code>class Event(models.Model):
      id PK, (automatically added)
      ts timestamp
  
  class MonitorEvent(Event):
      event_ptr_id FK -&gt; Event (automatically added)
      ts_copy timestamp
  </code></pre>
  
  <p>Both tables are partitioned by month using the timestamp. The timestamp, however is not part of the PK, since django does not allow multi-field PKs. (I have tried using joined PK fields without success <a href="https://stackoverflow.com/questions/19706043/django-save-object-based-on-pk-and-another-field">Stackoverflow Question</a>) Therefore I need a copy of the timestamp in the subclass to allow for a partitioned table of the subclass.</p>
  
  <p>Now to my problem: I would usually select all Events and MonitorEvents by executing:</p>
  
  <pre><code>Event.objects.all().select_related('monitorevent')
  </code></pre>
  
  <p>(I would, of course, add some filters as to not select all available events.) To my understanding this would result in a SQL Query like:</p>
  
  <pre><code>SELECT * FROM event LEFT JOIN monitorevent ON event.id = monitorevent.event_ptr_id;
  </code></pre>
  
  <p>which should join all event table partitions with all monitorevent table partitions. I would actually want something like:</p>
  
  <pre><code>SELECT * FROM event LEFT JOIN monitorevent ON event.id = monitorevent.event_ptr_id AND event.ts = monitorevent ts_copy;
  </code></pre>
  
  <p>which should allow the query planner to make use of the partition scheme to speed up the query.</p>
  
  <p>Is there a possibility in django to write a custom Manager that modifies the default select_related to include an additional field in the ON condition of the query? </p>
  
  <p>Another possibility for me would be to write a custom raw query that does the select_related manually, however according to this question <a href="https://stackoverflow.com/questions/9328251/django-select-related-in-raw-request" title="Django select related in raw request">Django select related in raw request</a> this does not seem to be possible except from the manual proposition made in the question.</p>
  
  <p>Of course I could also add the additional fields to the original event model and delete the submodel which would eliminate the problem altogether, but that would be my least favorite solution.</p>
  
  <p>I am using Django 1.5 in combination with a postgres DB.</p>
  
  <p>Thank you for your suggestions.</p>