Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Specify fields that should remain required #24

Open
kevinhu opened this issue Nov 15, 2023 · 4 comments
Open

Feature Request: Specify fields that should remain required #24

kevinhu opened this issue Nov 15, 2023 · 4 comments

Comments

@kevinhu
Copy link

kevinhu commented Nov 15, 2023

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.

@asaf
Copy link

asaf commented Dec 9, 2023

@JensHeinrich
Copy link

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

@ddanier
Copy link
Member

ddanier commented Apr 22, 2024

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?

@KrilleGH
Copy link

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 #39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants