Create a partial model definition of a pydantic model

2024-12-09

Sometimes I want to create a partial model definition of a pydantic model. For example, if I want to expose a database model from API, some of the fields I would like to keep in secret, or I just want to expose very few fields in a summary API. I don’t want to write model definition twice. I would like to write something like

ModelExposed = partial_model(ModelSource, 'ModelExposed', include=["title", "description"])

So I write a funciton as below to do it, including support of passing list of excluding fields, copying computed fields and validators.

def partial_model(model: pydantic.BaseModel,
	          partial_model_name: str,
                  include: list[str] | None = None,
                  exclude: list[str] | None= None,
                  doc: str | None= None,
                  module: str | None = None
                  ):
    fields = []
    for name, info in model.model_fields.items():
        if exclude and name in exclude:
            continue
        if include and name not in include:
            continue
        fields.append((name, info))
    new_model = pydantic.create_model(
	    partial_model_name,
	    __doc__=doc or model.__doc__,
	    __base__=model.__mro__[1:],
	    __module__=module or model.__module__,
	    **{name: (info.annotation, info) for (name, info) in fields}
    )
    for field in model.model_computed_fields:
        if field in model.__dict__:
            setattr(new_model, field, getattr(model, field))
    new_model.__pydantic_validator__ = model.__pydantic_validator__
    return new_model