Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a foreign key value instead of the id?

Tags:

I have the following models :

class FlightSchedule(models.Model):
    tail_number = models.ForeignKey(TailNumber, null=False)
    flight_number = models.CharField(max_length=30, null=False)
    flight_group_code = models.ForeignKey(FlightGroup, null=False)
    origin_port_code = models.ForeignKey(Port, null=False, related_name="Origin")
    destination_port_code = models.ForeignKey(Port, null=False, related_name="Destination")
    flight_departure_time = models.TimeField()
    start_date = models.DateField()
    end_date = models.DateField()

def __unicode__(self):
    return u'%s' % self.flight_number

class Meta:
    verbose_name_plural = "Flight Schedule"


class FlightScheduleDetail(models.Model):
    flight_date = models.CharField(max_length=30, null=False)
    flight_number = models.ForeignKey(FlightSchedule, null=False, related_name="flight_number_schedule")
    route_id = models.CharField(max_length=30, null=False, unique=True)
    flight_status = models.ForeignKey(Status, null=True, default=1)

def __unicode__(self):
    return u'%s' % self.route_id

class Meta:
    verbose_name_plural = "Flight Schedule Details"

and the serializer is as below :

class FlightScheduleDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = FlightScheduleDetail
        fields = '__all__'


class FlightScheduleSerializer(serializers.ModelSerializer):
    flight_number_schedule = FlightScheduleDetailSerializer(many=True)

    class Meta:
        model = FlightSchedule
        fields = ['tail_number', 'flight_number', 'origin_port_code', 'destination_port_code', 'flight_departure_time',
              'flight_number_schedule']

Here tail_number , flight_number is a foreign key. When I create an API, I get the response as the id of the fields. How can I display the name in the json?

My views.py is as below :

@api_view(['GET'])
def flight_schedule(request):
    schedule = FlightSchedule.objects.all()
    serializer = FlightScheduleSerializer(schedule, many=True)
    return Response(serializer.data)
like image 514
Sahana Prabhakar Avatar asked Apr 27 '17 12:04

Sahana Prabhakar


2 Answers

You can define the source with field_name in your serializer as follows.

I have used source='TailNumber.number'. Please use the right field_name in place of number

class UserProfileSerializer(serializers.ModelSerializer):
    tail_number = serializers.CharField(source='TailNumber.number', read_only=True)
    flight_number = ....(change as above)

    class Meta:
        model = FlightSchedule
        fields = ['tail_number', 'flight_number', 'origin_port_code', 'destination_port_code', 'flight_departure_time',
          'flight_number_schedule']
like image 199
Sijan Bhandari Avatar answered Sep 21 '22 10:09

Sijan Bhandari


You could simply add them as if they were attributes.

flight_number_str = serializers.ReadOnlyField(source='flight_number.flight_number')

First flight_number is the attribute of FlightScheduleDetail, then the one of FlightSchedule

and then add it to the list of fields fields = [..., 'flight_number_str']

Otherwise you may have a look at nested relationships in DRF which can offer more possibilities also.

like image 24
d6bels Avatar answered Sep 23 '22 10:09

d6bels