2016-11-11 73 views
0

我正在努力使用SlugRelatedField設置PrimaryKeyRelatedField。使用Django Rest框架反序列化ForeignKey上的SlugRelatedField

我的models.py的樣子:

class Airport(models.Model): 
    name = models.CharField(max_length=200, blank=True) 
    iata = models.CharField(max_length=20, blank=True) 
    <other unrelated fields> 

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) 

我的看法是這樣的:

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) 

如果我將serializers.py設置爲:

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

從貝我看串行器呈現爲:

FlightSerializer(): 
    flight_number = CharField(max_length=25, validators=[<UniqueValidator(queryset=Flight.objects.all())>]) 
    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) 

我能夠發佈與數據庫中的機場條目相匹配的原產地和目的地包,以創建新的航班。大。

但是,實際上我需要發佈機場代碼(即LAX)作爲出發地或目的地,並讓DRF計算出適當的實例。

我認爲這樣做的方法是使用SlugRelatedField。儘管付出了最大的努力,但我無法正確設置它。

我想設置serializers.py這樣的:

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

當我傳遞了一個有效的機場代碼(IATA)像KMS爲原點似乎被忽略。

這是什麼SlugRelatedField應該用於?

我做錯了什麼讓我的POST發送一個字符串,讓DRF看一個匹配數據庫條目的字段,並將其存儲爲與我僅僅傳遞一個有效的PK時一樣精美?

回答

0

從同事那裏得到了一些幫助。

問題是我只是錯誤配置SlugRelatedField。要查看機場代碼,我需要使用查詢集並設置slug_field。這就是全部:

class FlightSerializer(serializers.ModelSerializer): 

origin = serializers.SlugRelatedField(
    slug_field='iata', 
    queryset=Airport.objects.all() 
) 
class Meta: 
    model = Flight 
    fields = ('flight_number', 'origin', 'destination', 'scheduled_departure', 'scheduled_arrival') 
相關問題