2017-04-27 59 views
0

我有以下型號:如何顯示外鍵值而不是id?

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" 

以及串行化是如下:

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'] 

這裏tail_number,航班已外鍵。當我創建一個API時,我得到的答覆是字段的ID。我如何在json中顯示名稱?

我views.py是如下:

@api_view(['GET']) 
def flight_schedule(request): 
    schedule = FlightSchedule.objects.all() 
    serializer = FlightScheduleSerializer(schedule, many=True) 
    return Response(serializer.data) 
+0

你能修好你的縮進嗎? – moopet

+1

謝謝我的確... –

回答

1

您可以在串行定義與FIELD_NAME源,如下所示。我使用source='TailNumber.number'。請使用正確的FIELD_NAME代替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'] 
1

你可以簡單地添加他們,如果他們的屬性。

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

首先flight_numberFlightScheduleDetail屬性,那麼FlightSchedule

再一個它添加到fields = [..., 'flight_number_str']

否則,你可能在DRF在nested relationships看看字段列表,它可以也提供更多的可能性。

0

另一種選擇是在串行器中使用depth選項。這是指定嵌套的序列化 - doc

class AccountSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Account 
     fields = ('id', 'account_name', 'users', 'created') 
     depth = 1 

如果用戶是一個外鍵或多對多鍵串行將顯示用戶爲對象,而不是作爲重點。

depth選項應設置爲整數值,指示 關係的深度應該恢復 到平坦表示之前被遍歷。