其他分享
首页 > 其他分享> > pydantic模型

pydantic模型

作者:互联网

模型

1.1基本模型
from pydantic import BaseModel

class User(BaseModel):
    id: int  # 整形、必须
    name = 'Jane Doe'  # 通过默认值推断类型为字符串,不是必须,因为提供有默认值

    class Config:
        max_anystr_length = 10
        error_msg_templates = {
            'value_error.any_str.max_length': 'max_length:{limit_value}',
        }  


user = User(id='123')
1.2递归模型
from typing import List
from pydantic import BaseModel


class Foo(BaseModel):
    count: int
    size: float = None


class Bar(BaseModel):
    apple = 'x'
    banana = 'y'


class Spam(BaseModel):
    foo: Foo
    bars: List[Bar]


m = Spam(foo={'count': 4}, bars=[{'apple': 'x1'}, {'apple': 'x2'}])
1.3ORM模型
from typing import List
from pydantic import BaseModel, constr
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class BookOrm(Base):
    __tablename__ = 'companies'
    id = Column(Integer, primary_key=True, nullable=False)
    name= Column(String(20), index=True, nullable=False, unique=True)
    desc = Column(String(255), nullable=True, default='')


class BookModel(BaseModel):
    id: int
    name: constr(max_length=20)
    desc: constr(max_length=255)

    class Config:
        orm_mode = True


book_orm = BookOrm(
    id=1,
    name='格林童话',
    desc='《格林童话》是由德国语言学家雅各布·格林和威廉·格林兄弟收集、整理、加工完成的德国民间文学。《格林童话》里面约有200多个故事,大部分源自民间的口头传说,其中的《灰姑娘》《白雪公主》《小红帽》《青蛙王子》等童话故事较为闻名',
)

book_model = BookModel.from_orm(book_orm)
1.4通用模型
from typing import Generic, TypeVar, Optional, List

from pydantic import BaseModel, validator, ValidationError
from pydantic.generics import GenericModel


# 定义泛型
DataT = TypeVar('DataT')


class Error(BaseModel):
    code: int
    message: str


class DataModel(BaseModel):
    numbers: List[int]
    people: List[str]


class Response(GenericModel, Generic[DataT]):
    data: Optional[DataT]  # data参数可以是任意类型
    error: Optional[Error]


data = DataModel(numbers=[1, 2, 3], people=[])
error = Error(code=404, message='Not found')

print(Response[int](data=1).dict())
print(Response[str](data='value').dict())
print(Response(data=data, error=error).dict())
1.5模型字段不允许修改
from pydantic import BaseModel


class FooBarModel(BaseModel):
    a: str
    b: dict

    class Config:
        allow_mutation = False


# 一旦数据被初始化就不允许修改 allow_mutation = False
foobar = FooBarModel(a='hello', b={'apple': 'pear'})

try:
    foobar.a = 'different'
except TypeError as e:
    print(e)
1.6模型字段顺序
from pydantic import BaseModel, ValidationError


class Model(BaseModel):
    a: int
    b = 2
    c: int = 1
    d = 0
    e: float


print(Model.__fields__.keys())
# dict_keys(['a', 'c', 'e', 'b', 'd'])  # 报错也是保持这个字段顺序
1.7必填字段
from pydantic import BaseModel, Field


class Model(BaseModel):
    a: int
    b: int = ...
    c: int = Field(...)
1.8必填可选字段
from typing import Optional
from pydantic import BaseModel, Field


class Model(BaseModel):
    a: Optional[int]  # 可选,默认值None
    b: Optional[int] = ...
    c: Optional[int] = Field(...)
1.9动态默认值
from uuid import UUID, uuid4
from pydantic import BaseModel, Field


class Model(BaseModel):
    uid: UUID = Field(default_factory=uuid4)
    updated: datetime = Field(default_factory=datetime.utcnow)
2.0自动排除属性
from pydantic import BaseModel
from typing import ClassVar


class Model(BaseModel):
    id: int = 1
    _name: str = "jack"
    age: ClassVar = 23


print(Model.__fields__.keys())  # dict_keys(['id'])
m1 = Model()
print(m1._name)  # jack
print(m1.age)  # 23
2.1私有属性
from datetime import datetime
from pydantic import BaseModel, Field, PrivateAttr


class Model(BaseModel):
    updated: datetime = Field(default_factory=datetime.utcnow)
    # 私有属性需要更改或操作模型实例上的内部属性使用PrivateAttr而不是Field
    _updated: datetime = Field(default_factory=datetime.utcnow)
    _updated1: datetime = PrivateAttr(default_factory=datetime.utcnow)
    __updated2__: datetime = PrivateAttr(default_factory=datetime.utcnow)


print(m1.__private_attributes__)
# {'_updated1': ModelPrivateAttr(default=PydanticUndefined, default_factory=<built-in method utcnow of type object at 0x00007FFD07BAF720>), '__updated2__': ModelPrivateAttr(default=PydanticUndefined, default_factory=<built-in method utcnow of type object at 0x00007FFD07BAF720>)}
m1 = Model()
print(m1.updated)  # 2022-06-15 03:12:05.524165
print(m1._updated)  # default=PydanticUndefined default_factory=<built-in method utcnow of type object at 0x00007FFD07BAF720> extra={}
print(m1._updated1)  # 2022-06-15 03:12:05.524165
class Model(BaseModel):
    updated: datetime = Field(default_factory=datetime.utcnow)
    # 私有属性需要更改或操作模型实例上的内部属性使用PrivateAttr而不是Field
    _updated: datetime = Field(default_factory=datetime.utcnow)
    _updated1: datetime = PrivateAttr(default_factory=datetime.utcnow)
    __updated2__: datetime = PrivateAttr(default_factory=datetime.utcnow)

    class Config:
        # 此时_updated也成为了私有属性
        underscore_attrs_are_private = True


print(m1.__private_attributes__)
# {'_updated': ModelPrivateAttr(default=FieldInfo(default=PydanticUndefined, default_factory=<built-in method utcnow of type object at 0x00007FFD07BAF720>, extra={})), '_updated1': ModelPrivateAttr(default=PydanticUndefined, default_factory=<built-in method utcnow of type object at 0x00007FFD07BAF720>), '__updated2__': ModelPrivateAttr(default=PydanticUndefined, default_factory=<built-in method utcnow of type object at 0x00007FFD07BAF720>)}
2.2将数据解析为指定类型
from typing import List

from pydantic import BaseModel, parse_obj_as


class Item(BaseModel):
    id: int
    name: str


# `item_data` could come from an API call, eg., via something like:
# item_data = requests.get('https://my-api.com/items').json()
item_data = [{'id': 1, 'name': 'My Item'}]

items = parse_obj_as(List[Item], item_data)
print(items)
# [Item(id=1, name='My Item')]
2.3数据转换
from pydantic import BaseModel


class Model(BaseModel):
    a: int
    b: float
    c: str


print(Model(a=3.1415, b=' 2.72 ', c=123).dict())
# {'a': 3, 'b': 2.72, 'c': '123'}
2.4严格模式
from pydantic import BaseModel, conint, confloat, constr, ValidationError


class Model(BaseModel):
    a: conint(strict=True)
    b: confloat(strict=True)
    c: constr(strict=True)

try:
    print(Model(a=3.1415, b=' 2.72 ', c=123).dict())
except ValidationError as e:
    print(str(e))


'''
3 validation errors for Model
a
  value is not a valid integer (type=type_error.integer)
b
  value is not a valid float (type=type_error.float)
c
  str type expected (type=type_error.str)
'''

标签:__,name,default,模型,BaseModel,import,class,pydantic
来源: https://www.cnblogs.com/puffer/p/16377532.html