title: ForeignKey/OneToOneField referencing a set of unique_together fields?
body: <p>I would like to add some metadata to a certain object in my Django models. Normally this would be a good situation for a <a href="https://docs.djangoproject.com/en/1.8/ref/models/fields/#onetoonefield" rel="nofollow">OneToOneField</a> or even <a href="https://docs.djangoproject.com/en/1.8/topics/db/models/#multi-table-inheritance" rel="nofollow">Multi-table inheritance</a>, but in my case there's an additional special requirement:</p>
  
  <p>Due to the way data is synchronized in the overall system, the database storage for the actual "target" objects occasionally get re-created. That is, from time to time all the object rows get deleted and new ones are created in their place. But I need the metadata to persist.</p>
  
  <p>Besides its implicit primary key, each object also has a textual slug field that uniquely identifies it — but <strong>only</strong> within a single container. This slug is stable across the data being re-created, as is the container itself. So I'd like to use a combination of an object's slug and its container to associate my metadata, rather than the database primary key for the object.</p>
  
  <p>I found the <code>to_field</code> option on the <code>ForeignKey</code> model field, but if I set for example <code>to_field='id_in_container'</code> it complains since the <code>id_in_container</code> field is not unique on its own, only together with the <code>container</code> field.</p>
  
  <p>Is there a way to do something like this?</p>
  
  <pre><code>class Container(models.Model):
      name = models.CharField(max_length=255)
  
  class Object(models.Model):
      container = models.ForeignKey(Container)
      id_in_container = models.SlugField()
      name = models.CharField(max_length=255)
  
      class Meta:
          unique_together = ('container', 'id_in_container')
  
  # BUG: this won't actually work, `to_field` can't take a tuple!
  DESIRED_COMBO = ('container','id_in_container')
  
  class ObjectMetadata(models.model):
      object = models. OneToOneField(Object, to_field=DESIRED_COMBO)
      # … additional fields …
  </code></pre>
  
  <p>That is, have a <code>OneToOneField</code> or <code>ForeignKey</code> that targets multiple columns of a target model, when those columns are <code>unique_together</code>?</p>
  
  <p>I suppose I can implement some manual helpers, perhaps like this:</p>
  
  <pre><code>class ObjectMetadata(models.model):
      object_container = models.ForeignKey(Container)
      object_slug = models.SlugField()
      # … additional fields …
  
      @classmethod
      def get_for_object(cls, obj):
          return cls.objects.get(object_container=obj.container, object_slug=obj.id_in_container)
  </code></pre>
  
  <p>…but I'm wondering if there's a more appropriate way to model this.</p>