title: How to define a serializer for model with a foreign key (not pointing to a pk field but a unique field)?
body: <p>I am using django rest-framework</p>
  
  <p>There are two modes as below </p>
  
  <pre><code>class Service(ResourceModelBase):
      _id = models.UUIDField(unique=True, null=False, default=uuid.uuid1)
      access_key = models.CharField(max_length=200, unique=True, null=False)
      cluster = models.OneToOneField(Cluster, to_field='_id')
  
  class Cluster(ResourceModelBase):
      _id = models.UUIDField(unique=True, null=False, default=uuid.uuid1)
      name = models.CharField(max_length=200, unique=True, null=False)
  </code></pre>
  
  <p>The service have a foreign key point to the cluster unique key _id field <strong>please be noticed that _id is not the primary key, it is a unique key. This is 
   important to my later question</strong>. </p>
  
  <p>Then I define a serializer for the service model, so I can create and get the service data.</p>
  
  <pre><code>class ServiceSerializer(serializers.ModelSerializer):
      cluster_id = serializers.SlugRelatedField(queryset=models.Cluster.objects.all(), slug_field='_id')
      class Meta:
          model = models.RGW
          fields = ('_id', 'access_key', 'cluster_id')
  </code></pre>
  
  <p>Please be notice that I define the <strong>cluster_id as a SlugRelatedField not the PrimaryKeyRelatedField.</strong></p>
  
  <p>The reason to do so is I want to pass the cluster_id a parameter to create  service.</p>
  
  <p>If  I define cluster_id as primary key related field, the  framework  tend to think the cluster_id foreign key is pointing to a  primary  key column. But in my case it is not.  It is pointing  to a unique key field .</p>
  
  <p>So  I use SlugRelatedField(I `m not sure  if this is the right way to do). But  another  problem shows up.</p>
  
  <p>When  I send data to serializer. The  data  I send  in  is as below:</p>
  
  <pre><code>{'cluster_id': 'ef3f70cac9e111e89fd1f000ac192ced', 'access_key': 'aaaa'}
  </code></pre>
  
  <p>But data after being validated by  serailzier is </p>
  
  <pre><code>serializer.validated_data
  OrderedDict([('access_key', 'aaaa'), ('cluster_id', &lt;Cluster: Cluster object (2)&gt;)])
  </code></pre>
  
  <p>For some reason the cluster_id is  converted to cluster  object. So If I  run </p>
  
  <pre><code> serializer.save(serializer.validated_data) 
  </code></pre>
  
  <p>This will cause error. </p>
  
  <p>Of course I could re-write the save method. But I just want to know what  is the best way to do this. I feel I am using the framework  in a  wrong  way</p>