team23 / pydantic-partial

Create partial models from pydantic models
MIT License
51 stars 8 forks source link

Create Partial Model with fields excluded #36

Open ClayMav opened 3 months ago

ClayMav commented 3 months ago

For my use case, I am using a partial model as the input type to a patch operation. I am heavily utilizing openapi schemas and want the input type to my function to contain all minus a few fields from the data model. Below is an example to demonstrate.

class MyModel(BaseModel):
    name: str
    age: int
    create_time: datetime
    update_time: datetime
    id: str

class PartialMyModel(MyModel, PartialModelMixin): # I want to exclude create_time, update_time, and id
    pass

Ideally, I should be able to define a class or list of fields to exclude and pass that in so that I can remove those fields on all my models that I use in this way.

andonimichael commented 2 months ago

I was running into the same issue and found that Pydantic's model.dict has an exclude parameter to specify the fields to exclude. So if you are using this to limit the fields for updates in your CRUD API, you should just be able to exclude those fields before calling your ORM's upsert/update methods.