L
L
LakeForest2021-09-08 18:09:53
Python
LakeForest, 2021-09-08 18:09:53

fast API. How to add data processing to the text field that came in from pydantic import BaseModel?

from pydantic import BaseModel

class Request(BaseModel):
    text: str
    client_id: str


How to add text processing? I want to translate everything that comes in lower case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Grigoriev, 2021-09-08
@LakeForest

the simplest is to use a validator

from pydantic import BaseModel, validator


class Request(BaseModel):
    text: str
    client_id: str

    @validator('text')
    def str_to_lower(cls, text: str):
        return text.lower()


print(Request(text='Lower To Lower', client_id='Client'))

text='lower to lower' client_id='Client'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question