我如何在 pydantic 中使用具有 none 值的嵌套模型

How do i use nested model with none value in pydantic

from pydantic import BaseModel
from typing import List
class Emails(BaseModel):
    Type: int
    Value: str = None
    IsPrimary: bool

class User(BaseModel):
   Emails: List[Emails] = None

INPUT :- User('Emails': [{'Type': 0, 'Value': 'qwert@gmail.com', 'IsPrimary': true}, 
                        {'Type': 1, 'Value': 'qpoefk@outlook.com', 'IsPrimary': true}])

这里pydantic是一个python模块,Emailsclass继承了pydantic的BaseModel Type 是一个整数,Value 是一个字符串,IsPrimary 是布尔值。 将 json 字符串传递给 class

时出现错误
File "pydantic\main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 4 validation errors for users_record
Emails -> 0
  value is not None (type=type_error.not_none)
Emails -> 1
  value is not None (type=type_error.not_none)
Phones -> 0
  value is not None (type=type_error.not_none)
Phones -> 1
  value is not None (type=type_error.not_none)

您有相同的模型名称和字段名称Emails,这会导致错误。给一个不同的名字,例如:

class User(BaseModel):
    Emails: List[Email] = None