[Python] Python Pydantic: Optional non-nullable field

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    from pydantic import BaseModel

    class MyModel(BaseModel):
    id: int
    name: str # this should be optional


    MyModel(id=1)


    This raises a ValidationError. Setting name: str | None = None is unacceptable because the name cannot be null. It can be a string, or it can be unset, but it can never be null.

    How do I make this work?

    Continue reading...

Compartilhe esta Página