Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 把字典传给另一个班_Python_Python 3.x_Fastapi - Fatal编程技术网

Python 把字典传给另一个班

Python 把字典传给另一个班,python,python-3.x,fastapi,Python,Python 3.x,Fastapi,我有一个FastAPI应用程序,希望将字典从POST方法传递到子目录中的另一个模块。 我的main.py看起来像这样 from fastapi import FastAPI from pydantic import BaseModel from app.api import mymodule from app.api.mymodule import MyClass app = FastAPI(debug = True, version="0.0.1") class MyAPIModel(

我有一个FastAPI应用程序,希望将字典从POST方法传递到子目录中的另一个模块。 我的main.py看起来像这样

from fastapi import FastAPI
from pydantic import BaseModel
from app.api import mymodule
from app.api.mymodule import MyClass


app = FastAPI(debug = True, version="0.0.1")


class MyAPIModel(BaseModel):
    """
    Required input from frontend for IP Allocation
    """
    forename: str
    surname: str
    age: int

@app.get("/")
async def default():
    return {"Default root page"}


@app.get("/home")
async def home():
    return {"message": "Homepage for Application"}


@app.post("/ip")
async def ip(request: MyAPIModel):
    superhero = request.dict()
    superhero_request = MyClass.get_type(superhero)
Mymodule.py看起来像

import api

class MyClass:

    def __init__(self, superhero):
        self.mysetup = api(url, token)
        self.superhero_info = superhero

    """
    Here is where I want to access the dictionary
    and use it
    """

    def get_type(self):
        return self.superhero_info
我的POST请求是BaseModel的json dict

{
    "forename": "peter", 
    "surname": "parker",
    "age": 28
}
但这样做会产生以下错误

   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 550, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/usr/local/lib/python3.7/site-packages/fastapi/routing.py", line 148, in app
    dependant=dependant, values=values, is_coroutine=is_coroutine
  File "/usr/local/lib/python3.7/site-packages/fastapi/routing.py", line 101, in run_endpoint_function
return await dependant.call(**values)
  File "./app/main.py", line 36, in ip
Allocate.get_type(iprequest)
  File "./app/api/mymodule.py", line 21, in get_type
return self.superhero_info
AttributeError: 'dict' object has no attribute 'superhero'

是否有方法将字典传递给方法类,以便我可以在返回之前对其执行其他任务?

注释指出了正确的方向。完整的工作代码是

@app.post("/ip")
async def ip(request: MyAPIModel):
    mydata = request.dict()
    test = MyClass()
    results = test.get_type(mydata)
    return results

需要在子类和main.py中同时返回。在我看来,您调用的是静态函数,但是
get\u type()
不是静态的。您需要首先实例化MyClass对象,然后调用
get\u type()
。像这样,
MyClass().get\u type(myrequest)
我想你忘了在你的回溯中有错误名称的那一行添加了完整的错误@DonatienUsing MyClass()。get\u type(myrequest)返回null@thuyeintun但它是否再次导致错误?返回null很可能是因为您的请求没有
名字。如果没有被fastapi掩盖,那么不实例化MyClass就是一个简单的错误。关于从请求中获取数据的部分可以很容易地从fastapi的官方文档中收集,这是我以前从未使用过的。您只需要
返回MyClass().get\u type(request.dict())
。我的评论解决了将dict传递给其他模块的问题,在哪里获取dict完全是另一回事。