pydantic

FastAPI: how to read body as any valid json?

◇◆丶佛笑我妖孽 提交于 2021-02-18 14:00:47
问题 Sorry, not proficient in Python. I haven't found the docs for that use case. How can I get the request body, ensure its a valid Json ( any valid json , including numbers, string, booleans and nulls, not only objects and arrays) and get the actual Json. Using pydantic forces the Json to have a specific structure. 回答1: You can find nearly everything inside the Request object You are able to get request body with request.body() from fastapi import Request, FastAPI @app.post("/dummypath") async

FastAPI: how to read body as any valid json?

孤街醉人 提交于 2021-02-18 14:00:34
问题 Sorry, not proficient in Python. I haven't found the docs for that use case. How can I get the request body, ensure its a valid Json ( any valid json , including numbers, string, booleans and nulls, not only objects and arrays) and get the actual Json. Using pydantic forces the Json to have a specific structure. 回答1: You can find nearly everything inside the Request object You are able to get request body with request.body() from fastapi import Request, FastAPI @app.post("/dummypath") async

How to add a custom decorator to a FastAPI route?

拈花ヽ惹草 提交于 2021-02-10 05:21:26
问题 I want to add an auth_required decorator to my endpoints. ( Please consider that this question is about decorators, not middleware ) So a simple decorator looks like this: def auth_required(func): def wrapper(*args, **kwargs): if user_ctx.get() is None: raise HTTPException(...) return func(*args, **kwargs) return wrapper So there are 2 usages: @auth_required @router.post(...) or @router.post(...) @auth_required The first way doesn't work because router.post creates a router that saved into

pydantic and subclasses of abstract class

六眼飞鱼酱① 提交于 2021-01-29 07:56:35
问题 I am trying to use pydantic with a schema that looks as the following: class Base(BaseModel, ABC): common: int class Child1(Base): child1: int class Child2(Base): child2: int class Response(BaseModel): events: List[Base] events = [{'common':1, 'child1': 10}, {'common': 2, 'child2': 20}] resp = Response(events=events) resp.events #Out[49]: [<Base common=10>, <Base common=3>] It only took the field of the Base class and ignored the rest. How can I use pydantic with this kind of inheritance? I

Pydantic model does not validate on assignment with reproducable code

孤者浪人 提交于 2021-01-28 11:18:46
问题 When assigning an incorrect attribute to a Pydantic model field, no validation error occurs. from pydantic import BaseModel class pyUser(BaseModel): username: str class Config: validate_all = True validate_assignment = True person = pyUser(username=1234) person.username >>>1234 try_again = pyUser() pydantic.error_wrappers.ValidationError: [ErrorWrapper(exc=MissingError(), loc=('username',))] <class '__main__.pyUser'> How can I get pydantic to validate on assignment? 回答1: It is expected

Which type hint expresses that an attribute must not be None?

不想你离开。 提交于 2021-01-28 06:26:31
问题 In the following code, I need to declare my_attr as anything except None . What should I exchange Any for? from pydantic import BaseModel from typing import Any class MyClass(BaseModel): my_attr: Any 回答1: To achieve this you would need to use a validator, something like: from pydantic import BaseModel, validator class MyClass(BaseModel): my_attr: Any @validator('my_attr', always=True) def check_not_none(cls, value): assert value is not None, 'may not be None' return value But it's unlikely

fastapi form data with pydantic model

删除回忆录丶 提交于 2020-05-31 20:45:34
问题 I am trying to submit data from html forms and on the validate it with pydantic model. Using this code from fastapi import FastAPI, Form from pydantic import BaseModel from starlette.responses import HTMLResponse app = FastAPI() @app.get("/form", response_class=HTMLResponse) def form_get(): return '''<form method="post"> <input type="text" name="no" value="1"/> <input type="text" name="nm" value="abcd"/> <input type="submit"/> </form>''' class SimpleModel(BaseModel): no: int nm: str = "" @app