pydantic-partial icon indicating copy to clipboard operation
pydantic-partial copied to clipboard

Feature Request: Specify fields that should remain required

Open kevinhu opened this issue 2 years ago • 4 comments

Hi there, this is a really useful project! It would be great if we could pass a list of fields that should remain required in the resulting model—essentially, the opposite of the current ability to specify a subset of fields to be optional.

kevinhu avatar Nov 15 '23 03:11 kevinhu

Hey, maybe this is what you are looking for: https://github.com/team23/pydantic-partial/blob/main/pydantic_partial/partial.py#L22

asaf avatar Dec 09 '23 09:12 asaf

I think adding an required_fields keyword argument to the *_partial_model functions and removing them here would be an even cleaner solution as often you only need a few specific fields for PATCH requests for example, but need them always

JensHeinrich avatar Mar 01 '24 07:03 JensHeinrich

Current solution would be to use:

PartialModel = create_partial_model(
    Model,
    *(
        set(model_compat.model_fields.keys())
        - {"the", "fields", "you", "want", "to" "keep"}
    ),
)

Is that working for you?

ddanier avatar Apr 22 '24 14:04 ddanier

I wanted the partial model to just add a default value to all fields but keep whether the field is required or not from the full model.

This way all the fields may be omitted, but if they are given and aren't optional already in the full model Pydantic will reject null values.

Is that useful to you? See https://github.com/team23/pydantic-partial/pull/39

KrilleGH avatar Oct 25 '24 15:10 KrilleGH