我使用Pydantic的BaseModel创建我的工人阶级。除了带有验证器的方法之外,我还有其他自定义方法。我需要使用自定义函数的函式工具中的singledispatchmethod装饰器。例如,
class Foo(pydantic.BaseModel):
name: str
bar: int
baz: int
@functools.singledispatchmethod
def handle(self, command: Command) -> CommandResult:
pass
@celery.task
我有一个类似于dict的对象,比如:
data = {
# A lot of data here
'json_data_feed':
{'address':
{'name': 'home_sweet_home'}
}
# A lot of data here
}
我想创建只有几个字段的Pydantic模型。我正在尝试这样做:
class OfferById(pydantic.BaseModel):
short_address: st
我有两种型号的Article和Tag。Article有一个来自Tag的外键
class Tag(MyAbstractBaseModel):
name = fields.CharField(max_length=255, index=True)
class Article(MyAbstractBaseModel):
title = fields.CharField(max_length=255, index=True)
body = fields.CharField(max_length=255)
tag = fields.ForeignKeyField(mod
我是fastAPI新手,我正在尝试构建一个todo。我用乌龟-ORM。我认为该错误是由向todo部分输入空值导致的,尽管我希望将其设置为null或设置为默认值,这是一个空列表。这是与错误相关的代码。main.py
from typing import List
import os
from fastapi import FastAPI, Depends, HTTPException, status
from models import User, Todo, User_pydantic, UserIn_pydantic, Todo_pydantic, TodoIn_pydantic
fro
我试图使用pydantic的快速应用程序限制提供的日期少于今天。从中我看到了一个很好的函数condate,它有一个lt参数--这正是我所需要的。我的代码是:
from datetime import date
from pydantic import BaseModel, condate
class RiskData(BaseModel):
dob: condate(lt=date.today())
它提供了以下错误:
File "pydantic\main.py", line 342, in pydantic.main.BaseModel.__init__
当我通过create_model()动态地创建一个pydantic模型时,在某些情况下,update_forward_refs()无法找到相关的定义。
这样做是可行的:
from typing import List, Union
from pydantic import BaseModel
class Foo(BaseModel):
foo: List["Bar"]
Bar = Union[Foo, int]
Foo.update_forward_refs()
但是,对于NameError,以下内容(我认为应该是等价的)失败了:
from typing import
我正在尝试从python数据类生成JSON,这些类包含作为参数的其他数据类的列表。除了生成json模式之外,我并不真正关心pydantic的任何功能,因为我只是使用输出来自动生成web前端。因此,使用其他库的解决方案是可以的。
from typing import List
from dataclasses import dataclass
import pydantic
@dataclass()
class SomeParameters:
a: int = 5
@dataclass()
class SomeMoreParameters:
another: List[Som
我是Pydantic的新手,正在尝试了解如何/是否可以创建一个新的类实例。我已经通读了Pydantic文档,但找不到执行类似操作的示例。
我的python代码(在Pydantic之前)看起来像这样:
class Person:
def __init__(self, id):
db_result = get_db_content(id) #lookup id in a database and return a result
self.name = db_result['name']
self.birth_
我希望大家都平安无事。我正在尝试这段代码,让它正常工作。我有一个main.py文件和一个models.py文件,其中我在models.py文件中创建了一个类用户,并且我正在尝试将用户类从models.py文件导入到main.py文件。为此,我使用了乌龟ORM。我在命令提示符下收到错误消息"ImportError: cannot import name 'register_tortoise‘from 'tortoise.contrib.pydantic'“
**main.py**
from fastapi import FastAPI, Request, For
从pydantic docs中我理解了这一点: import pydantic
class User(pydantic.BaseModel):
id: int
name: str
class Student(pydantic.BaseModel):
semester: int
# this works as expected
class Student_User(User, Student):
building: str
print(Student_User.__fields__.keys())
#> dict_keys(['semest
我有这样的功能:
class Name(BaseModel):
name_id: str
first_name: str
last_name: str
def get_all_names() -> List[Name]:
names = []
try:
# this API returns a list of NAME objects
names = requests.get("some-url")
# I want to validate, that each NAME
我跟踪使用FastAPI HTML模板发送电子邮件,但它显示了一个与模板目录相关的错误。
conf = ConnectionConfig(
File "pydantic/env_settings.py", line 38, in pydantic.env_settings.BaseSettings.__init__
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validat
我在Kubuntu18.04上有 3.6.9。我已经使用安装了pip3 install fastapi。我正在尝试通过它的正式文档来测试这个框架,并且我正在使用它的指南的中。
在schemas.py中
from typing import List
from pydantic import BaseModel
class VerseBase(BaseModel):
AyahText: str
NormalText: str
class Verse(VerseBase):
id: int
class Config:
orm_mode = T
对于如下所示的Pydantic类,我希望通过应用replace操作来转换foo字段:
from typing import List
from pydantic import BaseModel
class MyModel(BaseModel):
foo: List[str]
my_object = MyModel(foo="hello-there")
my_object.foo = [s.replace("-", "_") for s in my_object.foo]
在创建对象时,如何在类中执行replace操作?如果没有Pyd