姜戈忍者 |正在尝试发送文件 |未找到 <class 'django.http.response.HttpResponse'> 的验证器,请参阅配置中的“arbitrary_types_allowed”

Djnago ninja | Trying to send file | no validator found for <class 'django.http.response.HttpResponse'>, see `arbitrary_types_allowed` in Config

好人,

我正在尝试实现文件下载功能。代码非常简单:

@api.get("/summary/", response=HttpResponse)
def report_summary(
    request: NinjaRequest, start_date: str, end_date: str
) -> HttpResponse:
    ...
    response = HttpResponse(
        output, # output is the file
        content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )
    response["Content-Disposition"] = f"attachment; filename={fname}" # fname is the name of the file
    return response

但是它在启动过程中给我一个错误提示:

RuntimeError: no validator found for <class 'django.http.response.HttpResponse'>, see `arbitrary_types_allowed` in Config

我不想设置arbitrary_types_allowed

现在我该如何解决这个问题?

根据:https://github.com/vitalik/django-ninja/issues/424#issuecomment-1099930539

删除这部分

, response=HttpResponse)

它确实有效