无法在 Django 中传递参数,给出 MultiValueDictKeyError
Can't pass parameters in Django, giving MultiValueDictKeyError
我已经回答了这类问题,但它们似乎与我的问题无关。
我有一个class定义如下:
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”)但得到:
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)
我已经回答了这类问题,但它们似乎与我的问题无关。
我有一个class定义如下:
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”)但得到:
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)