django-ninja
django-ninja copied to clipboard
value is not a valid list (type=type_error.list)
I need to output the value of return in the following function in the format of {"msg": "success", "code": 200, "data": qs}, but I can't solve it through various common sense, and will report various different mistake. The return JsonResponse({"msg": "success", "code": 200, "data": data}) can be returned normally, but it is invalid after using a custom renderer
class ORJSONRenderer(BaseRenderer):
media_type = "application/json"
def render(self, request, data, *, response_status):
return json.dumps({"data":data})
api = NinjaAPI(docs_decorator=staff_member_required, renderer=ORJSONRenderer())
How should this problem be solved.
@router.get("/copywriting_typelist", response=List[CopywritingListSchemaOut], summary="")
def movie_typelist(request):
qs = CopywritingType.objects.all()
data = [CopywritingListSchemaOut.from_orm(i).dict() for i in qs]
print(data)
return {"msg": "success", "code": 200, "data": data}
# return JsonResponse({"msg": "success", "code": 200, "data": data})
# return data
# return {"msg": "success", "code": 200, "data": json.dumps(data)}
# return data
# return {"msg": "success", "code": 200, "data": "data"}
# return {"msg": "success", "code": 200, "data": translate_text}
The above code reports an error after running
Traceback (most recent call last):
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\operation.py", line 100, in run
return self._result_to_response(request, result, temporal_response)
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\operation.py", line 196, in _result_to_response
result = response_model.from_orm(resp_object).dict(
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\schema.py", line 164, in from_orm
return super().from_orm(obj)
File "pydantic\main.py", line 574, in pydantic.main.BaseModel.from_orm
pydantic.error_wrappers.ValidationError: 1 validation error for NinjaResponseSchema
response
value is not a valid list (type=type_error.list)
well you define your response as a list:
response=List[CopywritingListSchemaOut]
so list is expected
if you need to that structure you need to define your response schema apropriately:
class Response(Schema):
msg: str
code: int
data: List[CopywritingListSchemaOut] # or just List
# and then
@router.get("/copywriting_typelist", response=Response) # !!!
def movie_typelist(request):
...
return {"msg": "success", "code": 200, "data": data}
well you define your response as a list:
response=List[CopywritingListSchemaOut]so list is expected
if you need to that structure you need to define your response schema apropriately:
class Response(Schema): msg: str code: int data: List[CopywritingListSchemaOut] # or just List # and then @router.get("/copywriting_typelist", response=Response) # !!! def movie_typelist(request): ... return {"msg": "success", "code": 200, "data": data}
Thanks a lot, how should I write if I need pagination. Using @paginate will report an error https://github.com/vitalik/django-ninja/issues/548
return {"msg": "success", "code": 200, "data": data}
Tested and still the same error,code show as below。
class CopywritingListSchemaOut(ModelSchema):
parent_category_id: int = Field(None, alias="parent_category_id")
class Config:
model = CopywritingType
model_fields = ["id", "type_name"]
class CopywritingListResponse(Schema):
msg: str
code: int
data: List[CopywritingListSchemaOut] # or just List
@router.get("/copywriting_typelist", response=CopywritingListResponse, )
def movie_typelist(request):
qs = CopywritingType.objects.all()
return {"msg": "success", "code": 200, "data": qs}
Traceback (most recent call last):
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\operation.py", line 100, in run
return self._result_to_response(request, result, temporal_response)
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\operation.py", line 196, in _result_to_response
result = response_model.from_orm(resp_object).dict(
File "D:\Program Files (x86)\Anaconda3\envs\django\lib\site-packages\ninja\schema.py", line 164, in from_orm
return super().from_orm(obj)
File "pydantic\main.py", line 574, in pydantic.main.BaseModel.from_orm
pydantic.error_wrappers.ValidationError: 1 validation error for NinjaResponseSchema
response -> data
value is not a valid list (type=type_error.list)
@ddzyx
qs = list(qs) # <--- fetch data and convert to list
return {"msg": "success", "code": 200, "data": qs}