我已經完成了這類問題的答案,但它們似乎與我的問題無關。
我有一個定義如下的類:
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'
需要幫忙。謝謝。
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/484498.html
上一篇:R:用NA繪制圖形
