Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
How can I create a Model that will take any other model or standard dict as a property using Pydantic's BaseModel?

Given a FastAPI Pydantic model MessageWrapper *untested:

class MessageWrapper(BaseModel):
    id: str = ids.get_time_sequential_uuid(datetime.datetime.utcnow())
    body: Union[DataMin, DataMax] = None
    created_by_app: Optional[AnyStr]
    created_by_app_version: Optional[AnyStr]
    created_at_utc: datetime = datetime.datetime.utcnow()

The MessageWrapper.body property expects a DataMin or DataMax model. How can I change this to take either an unvalidated standard Dict or ANY model (not just DataMin or DataMax).

My initial attempts ran into quite a few scenarios where Pydantic threw warnings for not properly defining the type of the body property. I expect to either pass standard dicts or models through this wrapper prior to the data being supplied through an API endpoint.

Ultimately the compiled nested dict would be received by the requestor and they could always expect to access the top level fields, while the body field could be any nested dictionary.

I am interested in functionality similar to Maxwell's Daemon where json data is inserted into a standard dictionary that includes metadata about the message itself.

It seems there would be times when it is OK to designate a property with Type (from typing import Type) to avoid validation, and other times where we'd always validate on all properties.

How can I create a Model that will take any other model or standard dict as a property using Pydantic's BaseModel?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda