django rest framework POST request fails with nested serialization
<p>My POST request to url <a href="http://127.0.0.1:8000/airlines/" rel="nofollow">http://127.0.0.1:8000/airlines/</a> fails</p>

<p>Here are models and corresponding serializers in my project. Initially I want to create Airlines information and then add flights later</p>

<p>Can someone please let me know where am I going wrong</p>

<p><strong>models.py</strong></p>

<pre><code>class AirlineFirm(models.Model):
    operator_name = models.CharField(max_length=30)

    def __unicode__(self):
       return "%s" % (self.operator_name)

class Flight(models.Model):

    flight_num = models.CharField(max_length=7, primary_key=True)
    src = models.CharField(max_length=20)
    dest = models.CharField(max_length=20)
    outbound_time = models.DateTimeField()
    inbound_time = models.DateTimeField()
    num_of_seats = models.IntegerField()
    ticketPrice = models.FloatField()
    delayed = models.BooleanField()
    airlinefirm = models.ForeignKey(AirlineFirm)
</code></pre>

<p><strong>serializers.py</strong></p>

<pre><code>class FlightSerializer(serializers.Serializer):

    flight_num = serializers.CharField(read_only=True)
    src = serializers.CharField()
    dest = serializers.CharField()
    outbound_time = serializers.DateTimeField()
    inbound_time = serializers.DateTimeField()
    num_of_seats = serializers.IntegerField()
    ticketPrice = serializers.FloatField()
    delayed = serializers.BooleanField()
    airlinefirm = serializers.RelatedField(read_only='True')
    #passengers = models.ManyToManyField(Passenger)


    def create(self, validated_data):
        return Flight.objects.create(**validated_data)

    def update(self, instance, validated_data):
        pass

class AirlineSerializer(serializers.ModelSerializer):

    flights = FlightSerializer(many=True)

    class Meta:
        model = AirlineFirm
        fields = ('operator_name','flights')

        def create(self, validated_data):
            flights_data = validated_data.pop('flights')
            airlinefirm = AirlineFirm.objects.create(**validated_data)

            for flight_data in flights_data:
                Flight.objects.create(airlinefirm=airlinefirm, **flight_data)
                return airlinefirm
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>@api_view(['GET','POST'])
def airline(request, format=None):

    if request.method == 'GET':
        airlines = AirlineFirm.objects.all()
        serializer = AirlineSerializer(airlines, many=True)
        return Response(serializer.data)

    if request.method == 'POST':
        #data = JSONParser().parse(request)
        serializer = AirlineSerializer(data=request.data)
        #import pdb;pdb.set_trace()
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            import pdb;pdb.set_trace()
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
</code></pre>

<p>When I send a POST request <a href="http://127.0.0.1:8000/airlines/" rel="nofollow">http://127.0.0.1:8000/airlines/</a> to my airlines view class I get 404 response</p>

<p><strong>http request</strong></p>

<pre><code>import json
import requests

payload = {'operator_name':'AmericanAirlines','flights':[]}
headers = {'Content-type':'application/json'}
r = requests.post('http://127.0.0.1:8000/',data=json.dumps(payload),headers=headers)
</code></pre>

<p><strong>Here is the error message</strong>:</p>

<p>AttributeError: Got AttributeError when attempting to get a value for field <code>flights</code> on serializer <code>AirlineSerializer</code>.
The serializer field might be named incorrectly and not match any attribute or key on the <code>AirlineFirm</code> instance.
Original exception text was: 'AirlineFirm' object has no attribute 'flights'.
[18/Feb/2016 13:43:58] "POST /airlines/ HTTP/1.1" 500 112039</p>