Today when you have a type created from a pydantic model, you can convert it back to pydantic, but not it's fields
I made this example using sqlmodel models
from pydantic import PositiveInt
from sqlmodel import (
Field,
SQLModel,
)
from strawberry import auto
from strawberry.experimental.pydantic import input as pydantic_input
class City(SQLModel):
id: int | None = Field(default=None, primary_key=True)
ibge: PositiveInt = Field(unique=True)
name: str
ddd: int | None = None
class AddressBase(SQLModel):
id: int | None = Field(default=None, primary_key=True)
zipcode: PositiveInt | None = None
neighborhood: str | None = None
complement: str | None = None
# coordinates: Coordinates | None = None
@pydantic_input(model=City)
class CityInput:
ibge: auto
name: auto
ddd: auto
@pydantic_input(model=AddressBase)
class AddressInput:
zipcode: auto
city: CityInput | None = None
address_input = AddressInput(city=CityInput(ibge=1111, name='str'))
print(address_input)
print(type(address_input.to_pydantic()))
print(address_input.to_pydantic())
print(type(address_input.city.to_pydantic()))
print(address_input.city.to_pydantic())
result:
AddressInput(city=CityInput(ibge=1111, name='str', ddd=None), zipcode=None)
<class '__main__.AddressBase'>
id=None zipcode=None neighborhood=None complement=None
<class '__main__.City'>
id=None ibge=1111 name='str' ddd=None
notice: when I print the whole address_input, it doesn't convert the field city, it's just dropped out
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too