Pydanticフィールドのみ
Ormarを使用すると、モデル内の通常のpydanticフィールドを宣言できるため、str、int、HttpUrl、PaymentCardNumberなどのすべての基本的なpydanticフィールドとカスタムpydanticフィールドにアクセスできます。
単一のフィールドだけでなく、ネストされたpydanticモデルにつながるフィールドを宣言することもできます。
これらのフィールドはデータベースに保存されないため(これがこれらのフィールドの意味です)、デフォルト値を設定するか、モデルの初期化中に意味のある値を指定する必要があります。
ormarがpydanticフィールドの値を解析できない場合、データベースからのデータのロード中に失敗し、宣言されたpydanticフィールドに必要な値が不足します。
次に、値を提供するオプションについて説明します。
もちろん、これらの一部またはすべてを1つのモデルにまとめることもできます。
オプションのフィールド
フィールドがオプションに設定されている場合、指定されていない場合はデフォルトで「なし」になります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | base_ormar_config=ormar. OrmarConfig(ベースの設定
metadata=sqlalchemy. MetaData(),
データベース=データベース。データベース(DATABASE_URL)、
)
class Model Test(ormar. Model):
base_or_config. copy()
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=200)
number:オプション[PaymentCardNumber]
test=Model Test(name="Test")
asserttest.name=="テスト"
assert test. numberはなし
test. number="123456789015"
Waittest.save()
test_check=await ModelTest. object.get()
asserttest_check.name=="テスト"
#ロード後、Noneに戻ります
assert test_check. numberはなし
|
デフォルト値を持つフィールド
デフォルト値を設定すると、初期化時およびデータベースの読み込み時に設定されます。デフォルト値を「なし」に設定することは、このフィールドを「オプション」に設定することと同じです。
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 | base_ormar_config=ormar. OrmarConfig(ベースの設定
metadata=sqlalchemy. MetaData(),
データベース=データベース。データベース(DATABASE_URL)、
)
class Model Test(ormar. Model):
base_or_config. copy()
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=200)
URL: HttpUrl="https://www.example.com"
test=Model Test(name="Test")
asserttest.name=="テスト"
assert test. url=="https://www.example.com"
test. url="https://www.sdta.ada.pt"
assert test. url=="https://www.sdta.ada.pt"
Waittest.save()
test_check=await ModelTest. object.get()
asserttest_check.name=="テスト"
#ロード後、デフォルトに戻ります。
assert test_check. url=="https://www.example.com"
|
デフォルトの工場出荷時の機能
デフォルトの_factory関数を設定することで、関数呼び出しの結果は初期化時とデータベースが読み込まれるたびに設定されます。
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
38
39
40
41
42
43 | from pydantic importフィールド, PaymentCardNumber
# ...
base_ormar_config=ormar. OrmarConfig(ベースの設定
metadata=sqlalchemy. MetaData(),
データベース=データベース。データベース(DATABASE_URL)、
)
カード番号=[
"123456789007",
"123456789015",
"123456789023",
"123456789031",
"123456789049",
]
def get_number():
return random. election(CARD_NUMBERS)
class ModelTest 2(ormar. Model):
base_or_config. copy()
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=200)
#関数を呼ばず、参照を渡すだけであることに注意してください。
number: PaymentCardNumber=フィールド(default_factory=get_number)
#まだ値を提供できることに注意してください
test=ModelTest 2(name="Test 2", number="4000000000000002")
asserttest.name=="Test 2"
assert test. number=="4000000000000002"
Waittest.save()
test_check=await Model Test 2. object.get()
asserttest_check.name=="Test 2"
#ロード値がCARD_NUMBERSのいずれかに設定された後
CARD_NUMBERSでtest_check. numberをアサートする
assert test_check. number!=test.number
|
__in it__のカスタム設定
super()in itメソッドを呼び出す前に、__in it__()メソッドでこのフィールドの値を指定できます。
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
38
39
40
41 | pydanticからBaseModelをインポート
# ...
base_ormar_config=ormar. OrmarConfig(ベースの設定
metadata=sqlalchemy. MetaData(),
データベース=データベース。データベース(DATABASE_URL)、
)
class PydanticTest(BaseModel):
タグ: str
bb: int
class ModelTest 3(ormar. Model):
base_or_config. copy()
#カスタムin it関数を提供する
def__in it__(self,**kwargs):
#デフォルト値のない必須フィールドに値を追加する
kwargs["pydantic_test"]=PydanticTest(aa="random", bb=42)
ormar. Model in itを呼び出すことを忘れないでください!
super().__in it__(**kwargs)
id: int=ormar.Integer(プライマリキー=True)
name: str=ormar.String(max_length=200)
pydantic_testについて
test=ModelTest 3(name="Test 3")
asserttest.name=="Test 3"
アサートテスト。pydantic_test.bb==42
test. pydantic.aa="新しい値"
assert test. pydantic.aa=="新しい値"
Waittest.save()
test_check=await Model Test 3. object.get()
asserttest_check.name=="Test 3"
#ロード後、in itで提供された値に戻ります。
assert test_check. pydantic_test.aa=="random"
|
!!!警告上記のいずれかの方法で値を指定しないと、データベースから読み込まれたときにValidationErrorが発生します。