You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If anyone is interested here is a function filling my needs.
frompydanticimportBaseModel, FieldfromtypingimporList, Typedefmodel_subset(
class_name: str,
Model: Type[BaseMode]l,
fields: List[str],
BaseClasses: tuple=(BaseModel,)
) ->Type[BaseMode]l:
""" From a pydantic model class create a new model class with a subset of fields Args: class_name (str): name of the create class model Model (BaseModel): Model root fields (List[str]): list of Model field names BaseClasses (optional, tuple): base classes of the new class default is (BaseModel,) """annotations= {}
new_fields= {}
forfield_nameinfields:
field=Model.__fields__[field_name]
fi=field.field_infokwargs=dict(
description=fi.description,
ge=fi.ge,
gt=fi.gt,
le=fi.le,
lt=fi.lt,
max_items=fi.max_items,
max_length=fi.max_length,
min_items=fi.min_items,
min_length=fi.min_length,
multiple_of=fi.multiple_of,
title=fi.title,
)
kwargs.update(fi.extra)
new_field=Field(fi.default, **kwargs)
annotations[field.name] =field.type_new_fields[field.name] =new_fieldnew_class=type(class_name, BaseClasses, new_fields)
new_class.__annotations__=annotationsreturnnew_class
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
If anyone is interested here is a function filling my needs.
Beta Was this translation helpful? Give feedback.
All reactions