使用 Django REST 发出 POST 请求时,validated_data 为空

我正在尝试使用 Django REST 框架在我的数据库中创建一个新条目,但我能够成功发送请求,但请求validated_data为空:


# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.shortcuts import render

from rest_framework import viewsets

from django.shortcuts import get_object_or_404, get_list_or_404

from .models import GetPurchases, CarFax

from .serializers import PurchasesSerializer

from .serializers import CarFaxSerializer

from rest_framework.response import Response


# Create your views here.



class GetCarFax(viewsets.ModelViewSet):

    ''' This view will be used for POSTing new carfax reports to the database '''


    queryset = CarFax.objects.all()

    serializer_class = CarFaxSerializer

    # authentication_classes = []

    permission_classes = []

    #print('TEST')


    def list(self, request):


        # accessed at url: ^api/v1/carfax/$

        queryset = CarFax.objects.all()

        serializer = CarFaxSerializer(queryset, many=True)


        return Response(serializer.data)


    def retrieve(self, request, pk=None, *args, **kwargs):

        # accessed at url: ^api/v1/retrieve/{pk}/$

        queryset = CarFax.objects.all()

        record = get_list_or_404(queryset, vin__exact=pk)

        serializer = CarFaxSerializer(record, many=True)


        return Response(serializer.data)


    def create(self, request, **validated_data):

        print('TEST')

        print(request.data)

        print(validated_data)

        serializer = CarFaxSerializer(CarFax.objects.create(**validated_data))

        headers = self.get_success_headers(serializer.data)

        print(serializer.data)


        print(headers)

        return Response(serializer.data, headers=headers)

序列化程序.py


class CarFaxSerializer(serializers.ModelSerializer):


    class Meta:

        model = CarFax

        fields = ('vin', 'structural_damage', 'total_loss',

                  'accident', 'airbags', 'odometer', 'recalls',

                  'last_updated')

当我打印时,request.data我可以看到我发送的数据。然而validated_data是空的,这是我用来创建模型的字典。


汪汪一只猫
浏览 554回答 2
2回答

慕工程0101907

首先,你的GetCarFax类看起来很乱:(因为你覆盖了大多数方法,但它没有为视图提供任何优势。所以将你的GetCarFax视图类更改为下面,class GetCarFax(viewsets.ModelViewSet):    """    This view will be used for POSTing new carfax reports to the database    """    queryset = CarFax.objects.all()    serializer_class = CarFaxSerializer    # authentication_classes = []    permission_classes = []    lookup_field = 'myfieldname'这几行代码将为您处理所有CRUD操作:)

红颜莎娜

接受的答案是一个很好的重构,但它没有回答问题,因此可能对未来的读者没有帮助(比如我自己,刚刚经历过这个)。真正的原因验证的数据是空是因为当你覆盖.create在ModelViewSet你消灭串行模型字段绑定,必须明确指定它们,你会经常串行器。CarFax 序列化程序应该像这样定义:class CarFaxSerializer(serializers.ModelSerializer):    vin = serializers.CharField(23),    structural_damage = serializers.BooleanField(),    total_loss = serializers.BooleanField(),    accident = serializers.BooleanField(),    airbags = serializers.BooleanField(),    odometer = serializers.IntegerField(),    recalls = serialziers.BooleanField(),    last_updated = serializers.DateField(),    class Meta:        model = CarFax        fields = ('vin', 'structural_damage', 'total_loss',                  'accident', 'airbags', 'odometer', 'recalls',                  'last_updated')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python