pydantic 2. XXに基づいて0.20.0に移行する
バージョン0.20.0pydantic v 2. XXをサポートし、開発者に大幅な速度の向上(検証とシリアル化はrustで書かれています)とよりクリーンなAPIを提供し、pydantic v.1を放棄します。
XXのサポートormarには、pydanticで行われた変更に対応するインターフェースの変更があります。
大きな変化
バージョン>=への移行0.20.0動作するには複数の変更が必要です。
オマモデル構成
ここで、すべてのormarモデルには、Metaクラスを定義するのではなく、OrmarConfigクラスのインスタンスであるormar_configパラメータが必要です。このプロパティは、ormar_configという名前で、configクラスのインスタンスである必要があることに注意してください。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | データベースのインポート
インポートまたは
sqlalchemyをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
#ormar<0.20
class Album(ormar. Model):
クラスMeta:
データベース
メタデータ
tablename="アルバム"
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
お気に入り: bool=ormar.Boolean(default=False)
#ormar>=0.20
class AlbumV 20(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
tablename="アルバム_v 20"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
お気に入り: bool=ormar.Boolean(default=False)
|
OrmarConfig API/パラメータ
ormar_configはMetaクラスで提供されているものと同じ設定セットを公開します。これは、次のいずれかのパラメータを使用して設定を初期化できることを意味します。
| メタデータ:オプション[sqlalchemy. MetaData]
データベース:オプション[データベース.データベース]
engine:オプション[sqlalchemy. engine.Engine]
tablename:オプション[str]
order_by:オプション[リスト[str]]
アブストラクト: bool
除外_親フィールド:オプション[リスト[str]]
queryset_class:タイプ[QuerySet]
エクストラ:エクストラ
制約:オプション[リスト[ColumnCollectionConstraint]]
|
コードの重複を減らし、開発を簡素化するために、基本構成を作成し、モデルごとにコピーを提供することをお勧めします。OrmarConfigは、このための便利なコピー方法を提供します。
copyメソッドはOrmarConfig in itと同じ引数を受け付けるため、必要に応じて上書きできますが、デフォルトでは、tablename、order_by、およびデフォルトでクリアされる制約を除いて、既存のプロパティを返します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 | データベースのインポート
インポートまたは
sqlalchemyをインポート
base_ormar_config=ormar. OrmarConfig(ベースの設定
Database("sqlite:///db. sqlite"),
Metadata=sqlalchemy. MetaData()
)
class AlbumV 20(ormar. Model):
base_ormer_config. copyを使用します。
tablename="アルバム_v 20"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
class TrackV 20(ormar. Model):
base_ormer_config. copyを使用します。
tablename="track_v 20"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
|
optionsフィールドパラメータはサポートされなくなりました。
バージョン0.20以前は、任意の既存のormarフィールドにオプションパラメータを指定して、受け入れ可能な値を制限することができました。この機能は削除され、この目的のために設計されたormar. Enumフィールドを使用する必要があります。
データベースフィールドタイプ(つまり、整数セグメント)を保持する場合は、いつでもカスタムベリファイアを記述できます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 | データベースのインポート
インポートまたは
sqlalchemyをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
#ormar<0.20
class Artist(ormar. Model):
クラスMeta:
データベース
メタデータ
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
国: str=ormar.String(デフォルト=False、max_length=50、選択肢=["UK","US","Vietnam","コロンビア"])
#ormar>=0.20
from enumインポートEnum
class Country(str, Enum):
UK="UK"
US="US"
ベトナム=「ベトナム」
コロンビア="コロンビア"
class ArtistV 20(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
tablename="artists_v 20"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
Country: Country=ormar.Enum(enum_class=Country)
|
pydantic_onlyはフィールドパラメータをサポートしなくなりました
pydantic_onlyフィールドは廃止され、v 0.20で削除されました。Ormarは通常のpydanticモデルと同様にpydanticフィールドを定義することができます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | データベースのインポート
インポートまたは
sqlalchemyをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
#ormar<0.20
class Dish(ormar. Model):
クラスMeta:
データベース
メタデータ
tablename="皿"
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
cook: str=ormar.String(max_length=40,pydantic_only=True,default="sam")
#ormar>=0.20
class DishV 20(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
tablename="dishes_v 20"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
cook: str="sam"#これは通常のピダンティック場です
|
プロパティ_fieldデコレータはサポートされなくなりました
property_fieldデコレータは、モデルのディクショナリ/シリアル化されたjson表現に含まれる計算フィールドを渡すメソッドを提供するために使用されます。pydanticバージョン2. Xではこの可能性が導入されていますので、ここでpydanticネイティブバージョンに切り替える必要があります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 | データベースのインポート
インポートまたは
sqlalchemyをインポート
pydanticをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
#ormar<0.20
class Employee(ormar. Model):
クラスMeta:
データベース
メタデータ
id: int=ormar.Integer(プライマリキー=True)
first_name: str=ormar.String(max_length=100)
last_name: str=ormar.String(max_length=100)
@ormar. properties_field()
def full_name(self)->str:
return f"{self.first_name}{self.last_name}"
#ormar>=0.20
class EmployeeV 20(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
)
id: int=ormar.Integer(プライマリキー=True)
first_name: str=ormar.String(max_length=100)
last_name: str=ormar.String(max_length=100)
@pydantic. computed_field()
def full_name(self)->str:
return f"{self.first_name}{self.last_name}"
|
廃止されたメソッド
以下にリストされているすべてのメソッドは廃止され、ormar 0.30で削除されます。
dict()はmodel_dump()になります
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 | データベースのインポート
インポートまたは
sqlalchemyをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
class Album(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
tablename="アルバム"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
お気に入り: bool=ormar.Boolean(default=False)
アルバム名: Dark Side of the Moon
#ormar<0.20
アルバム_dict=アルバム. dict()
#ormar>=0.20
new_アルバム_dict=アルバム.モデルダンプ()
|
引数はそのまま、つまりインクルード、除外などであることに注意してください。
json()はmodel_dump_json()になります
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 | データベースのインポート
インポートまたは
sqlalchemyをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
class Album(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
tablename="アルバム"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
お気に入り: bool=ormar.Boolean(default=False)
アルバム名: Dark Side of the Moon
#ormar<0.20
アルバム_json=アルバム. json()
#ormar>=0.20
new_アルバム_dict=アルバム. model_dump_json()
|
引数はそのまま、つまりインクルード、除外などであることに注意してください。
構築()をmodel_構築()に変更する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | データベースのインポート
インポートまたは
sqlalchemyをインポート
データベース=データベース。データベース("sqlite:///db. sqlite")
Metadata=sqlalchemy. MetaData()
class Album(ormar. Model):
ormar_config=ormar. OrmarConfig
データベース=データベース、
metadata=メタデータ,
tablename="アルバム"
)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=100)
お気に入り: bool=ormar.Boolean(default=False)
パラメータ={
タイトル: Dark Side of the Moon
"お気に入り": True,
}
#ormar<0.20
アルバム=アルバム.構築(**params)
#ormar>=0.20
アルバム=Album. model_structure(**params)
|
構築の詳細については、pydanticドキュメントを参照してください。