Deserializing SlugRelatedField on ForeignKey with Django Rest Framework
<p>I am struggling to set a PrimaryKeyRelatedField using a SlugRelatedField.</p>

<p>My models.py looks like:</p>

<pre><code>class Airport(models.Model):
    name = models.CharField(max_length=200, blank=True)
    iata = models.CharField(max_length=20, blank=True)
    &lt;other unrelated fields&gt;

class Flight(models.Model):
    flight_number = models.CharField(max_length=25, blank=False, unique=True)
    origin = models.ForeignKey(Airport, related_name='origin', null=True)
    destination = models.ForeignKey(Airport, related_name='destination', null=True)
    scheduled_departure = models.DateTimeField(null=True)
    scheduled_arrival = models.DateTimeField(null=True)
</code></pre>

<p>My view looks like:</p>

<pre><code>class FlightList(APIView):
    # List all flights, or create a new flight
    queryset = Flight.objects.all()
    serializer_class = FlightSerializer

    def get(self, request, format=None):
        flights = Flight.objects.all()
        serializer = FlightSerializer(flights, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = FlightSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            print serializer.validated_data
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
</code></pre>

<p>If I set my serializers.py to:</p>

<pre><code>class FlightSerializer(serializers.ModelSerializer):
    class Meta:
        model = Flight
        fields = ('flight_number', 'origin', 'destination', 'scheduled_departure', 'scheduled_arrival')
</code></pre>

<p>From the shell I see the serializer renders to:</p>

<pre><code>FlightSerializer():
    flight_number = CharField(max_length=25, validators=[&lt;UniqueValidator(queryset=Flight.objects.all())&gt;])
    origin = PrimaryKeyRelatedField(allow_null=True, queryset=Airport.objects.all(), required=False)
    destination = PrimaryKeyRelatedField(allow_null=True, queryset=Airport.objects.all(), required=False)
    scheduled_departure = DateTimeField(allow_null=True, required=False)
    scheduled_arrival = DateTimeField(allow_null=True, required=False)
</code></pre>

<p>I am able to POST origin and destination pk's that match up with Airport entries in the database to create new flights. Great.</p>

<p>However, I actually need to post the airport code, (i.e.) LAX as the origin or destination, and have DRF figure out the appropriate instance.</p>

<p>I think the way to do this is using SlugRelatedField. Despite best efforts, I'm unable to set this up properly.</p>

<p>I am trying to set serializers.py like this:</p>

<pre><code>class FlightSerializer(serializers.ModelSerializer):
    origin = serializers.SlugRelatedField(
        many=True,
        read_only=True,
        slug_field='iata',
    )
    class Meta:
        model = Flight
        fields = ('flight_number', 'origin', 'destination', 'scheduled_departure', 'scheduled_arrival')
</code></pre>

<p>When I pass up a valid airport code (iata) like KMS as the origin it seems to get ignored.</p>

<p>Is this what SlugRelatedField is supposed to be used for? </p>

<p>What am I doing wrong to allow my POST to send up a string, have DRF look at a field for matching db entries and store that as beautifully as it does when I simply pass up a valid PK?</p>