无法在 Django 中传递参数,从而给出 MultiValueDictKeyError



我已经看过这类问题的答案,但它们似乎与我的问题无关。

我有一个定义如下的类:

class TrainModel(APIView):
permission_classes = (IsAuthenticated,)
def post(self, request, *args):
print("I am here")
params = json.loads(request.POST["params"])
print(params)
#setup_instance.apply_async((params,), queue="local")
fit_model(params, "")
model_name = "{}-{}-{}-{}".format(
params["match_slug"], params["match_id"], params["model_type"], params["version"])
response = {
"success": True,
"message": "Model is being trained please wait",
"data": {"model_name": model_name}
}
return JsonResponse(response)

还需要其他输入:

params["meta"]["xgb"]params["combined"]

并按如下方式传递它们:

import requests
import json
headers = {'Authorization': 'Token $TOKEN', 'content-type': 'application/json'}
url = 'http://somesite.com/train/'
params = {  "match_id": 14142,
"model_type": "xgb",
"version": "v2",
"match_slug": "csk-vs-kkr-26-mar-2022",
"meta": {
"xgb": {
"alpha": 15,
"max_depth": 4,
"objective": "reg:linear",
"n_estimators": 53,
"random_state": 0,
"learning_rate": 0.1,
"colsample_bytree": 0.4
},
"catboost": {
"cat_cols": [
"batting order_lag_1",
"venue",
"against",
"role",
"bowlType"
],
"eval_metric": "RMSE",
"random_seed": 42,
"logging_level": "Silent",
"loss_function": "RMSE"
}
},
"image_id": "SOME_ID",
"combined": 0
}
resp = requests.post(url, params=params, headers=headers)
print(resp)

我在Postman中做了同样的尝试,将所有这些参数放在Body"(使用"raw")但是get:

Exception Type: MultiValueDictKeyError
Exception Value: 'params'

需要帮助。谢谢。

class TrainModel(APIView):
permission_classes = (IsAuthenticated,)
def post(self, request):
data = request.data # I would suggest using a serializer and it automatically validates your data 
fit_model(data, "")
model_name = "{}-{}-{}-{}".format(
data.get("match_slug"), data.get("match_id"), data.get("model_type"), data.get("version"))
response = {
"success": True,
"message": "Model is being trained please wait",
"data": {"model_name": model_name}
}
return Response(response, status=status.HTTP_200_OK)

相关内容

  • 没有找到相关文章

最新更新