代码之家  ›  专栏  ›  技术社区  ›  Carlos Ochoa

如何在访问我的Schema字段?

  •  0
  • Carlos Ochoa  · 技术社区  · 2 年前

    我一直在用PyTest进行一些测试,其中之一包括测试我收到的模式及其字段,我的模式看起来像:

    class MvnoaBase(BaseModel):
        """
        Shared properties for mvnoa schema
        """
        name: str = Field(min_length=1, max_length=100)
        is_active: bool
    

    我尝试通过model_fields访问:

    def test_validate_length_create_mvnoa_schema(client: TestClient, mvnoa_in, expected_code):
        mvnoa_in = schemas.MVNOACreate(name=mvnoa_in['name'],
                                       is_active=mvnoa_in['is_active'])
        mvnoa_db = client.post('api/v1/mvnoa/', json=mvnoa_in.model_dump())
        fields = schemas.MVNOACreate.model_fields
        assert len(mvnoa_in['name']) <= fields['name'].metadata[1].max_length
    

    但是打印字段给出了这个dict,有其他方法吗?

    {'name': FieldInfo(annotation=str, required=True, metadata=[MinLen(min_length=1), MaxLen(max_length=100)]), 'is_active': FieldInfo(annotation=bool, required=True)}
    
    0 回复  |  直到 2 年前