I have gone through answers of this type of question but they don’t seem relevant for my problem.
I have a class defined as follows:
JavaScript
x
18
18
1
class TrainModel(APIView):
2
permission_classes = (IsAuthenticated,)
3
4
def post(self, request, *args):
5
print("I am here")
6
params = json.loads(request.POST["params"])
7
print(params)
8
#setup_instance.apply_async((params,), queue="local")
9
fit_model(params, "")
10
model_name = "{}-{}-{}-{}".format(
11
params["match_slug"], params["match_id"], params["model_type"], params["version"])
12
response = {
13
"success": True,
14
"message": "Model is being trained please wait",
15
"data": {"model_name": model_name}
16
}
17
return JsonResponse(response)
18
It also requires other inputs:
params["meta"]["xgb"]
params["combined"]
And I pass them as follows:
JavaScript
1
41
41
1
import requests
2
import json
3
4
headers = {'Authorization': 'Token $TOKEN', 'content-type': 'application/json'}
5
url = 'http://somesite.com/train/'
6
7
params = { "match_id": 14142,
8
"model_type": "xgb",
9
"version": "v2",
10
"match_slug": "csk-vs-kkr-26-mar-2022",
11
"meta": {
12
"xgb": {
13
"alpha": 15,
14
"max_depth": 4,
15
"objective": "reg:linear",
16
"n_estimators": 53,
17
"random_state": 0,
18
"learning_rate": 0.1,
19
"colsample_bytree": 0.4
20
},
21
"catboost": {
22
"cat_cols": [
23
"batting order_lag_1",
24
"venue",
25
"against",
26
"role",
27
"bowlType"
28
],
29
"eval_metric": "RMSE",
30
"random_seed": 42,
31
"logging_level": "Silent",
32
"loss_function": "RMSE"
33
}
34
},
35
"image_id": "SOME_ID",
36
"combined": 0
37
}
38
39
resp = requests.post(url, params=params, headers=headers)
40
41
print(resp)
I try the same in Postman by putting all these parameters in “Body” (using “raw”) but get:
JavaScript
1
3
1
Exception Type: MultiValueDictKeyError
2
Exception Value: 'params'
3
Need help. Thanks.
Advertisement
Answer
JavaScript
1
19
19
1
class TrainModel(APIView):
2
permission_classes = (IsAuthenticated,)
3
4
def post(self, request):
5
data = request.data # I would suggest using a serializer and it automatically validates your data
6
7
fit_model(data, "")
8
model_name = "{}-{}-{}-{}".format(
9
data.get("match_slug"), data.get("match_id"), data.get("model_type"), data.get("version"))
10
11
response = {
12
"success": True,
13
"message": "Model is being trained please wait",
14
"data": {"model_name": model_name}
15
}
16
17
return Response(response, status=status.HTTP_200_OK)
18
19