Home>

There is a result from an API request, for example, like this:

{
  "result": [
    {
      "info": "foo",
      "service_type": "type1"
    },
    {
      "info": "bar",
      "service_type": "type2"
    },
    {
      "info": "base",
      "service_type": "typy3"
    },
    {
      "info": "zzz",
      "service_type": "type4"
    },
    ...
  ]
}

There are classes:

class Server(BaseModel):
    info: str
    service_type:str
    class Config:
        extra= 'ignore'
classServers(BaseModel):
    result:list[Server]
    class Config:
        extra= 'ignore'

Is it possible using pydanticto filter data by the values ​​of a certain field? With extra= 'ignore' I removed all the data I didn't need from the query result.
But then you need to exclude by certain values ​​for the field service_type.
For example, you don't need to save to the model if the value of service_typeis type4 or type5. Those. you don't need to throw an exception, just don't save them and only get the result with service_typecontaining type1, type2 or type3.

Get everything and then filter by the service_type value.

insolor2022-02-05 07:21:13

Yes, that's what I ended up doing. Thank you.

Тор2022-02-05 07:21:13