我将以下JSON GET请求发送到定义产品配置的服务器:
{'currency': ['"GBP"'], 'productConfig': ['[{"component":"c6ce9951","finish":"b16561c9"},{"component":"048f8bed","finish":"b4715cda"},{"component":"96801e41","finish":"8f90f764"},{"option":"6a202c62","enabled":false},{"option":"9aa498e0","enabled":true}]']}
我正在尝试通过DRF验证这一点,我有以下配置:
views.py
class pricingDetail(generics.ListAPIView):
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
parser_classes = (JSONParser,)
def get(self, request, *args, **kwargs):
pricingRequest = pricingRequestSerializer(data=request.query_params)
if pricingRequest.is_valid():
return Response('ok')
serializers.py
class pricingComponentSerializer(serializers.ModelSerializer):
class Meta:
model = Component
fields = ('sku',)
class pricingFinishSerializer(serializers.ModelSerializer):
class Meta:
model = Finish
fields = ('sku',)
class pricingOptionSerializer(serializers.ModelSerializer):
class Meta:
model = ProductOption
fields = ('sku',)
class pricingConfigSerializer(serializers.ModelSerializer):
finish = pricingFinishSerializer(read_only=True, many=True)
component = pricingComponentSerializer(read_only=True, many=True)
option = pricingOptionSerializer(read_only=True, many=True)
enabled = serializers.BooleanField(read_only=True)
class pricingCurrencySerializer(serializers.ModelSerializer):
class Meta:
model = Currency
fields = ('currencyCode',)
class pricingRequestSerializer(serializers.Serializer):
config = pricingConfigSerializer(read_only=True)
currency = pricingCurrencySerializer(read_only=True)
正如您所看到的,我正在尝试通过使用内联序列化程序在同一请求中验证多个模型。
我的问题
上面的代码允许所有内容都通过is_valid()
(即使我发出无效请求,它也会返回一个空的validated_data
(OrderedDict([])
)值。
我做错了什么?
额外信息
生成GET请求的JS如下:
this.pricingRequest = $.get(this.props.pricingEndpoint, { productConfig: JSON.stringify(this.state.productConfig), currency: JSON.stringify(this.state.selectedCurrency) }, function (returnedData, status) {
console.log(returnedData);
答案 0 :(得分:0)
我目前没有计算机来浏览源代码,但您可能需要检查序列化程序上的read_only
参数。 Afaik这仅适用于在回复中显示数据。
您可以使用ipdb轻松检查(ipython调试器) 只是说:
import ipdb; ipdb.set_trace()
您想要开始调试,启动服务器并启动请求。