Giter VIP home page Giter VIP logo

Comments (8)

sydney-runkle avatar sydney-runkle commented on September 28, 2024 5

@tteguayco,

We can leave this open - it's a pretty heavily requested feature, so I think it'll be great to leave this open as a feature request. I'll work on an API design. I think this can fit into the scope of v2.9!

from pydantic.

tteguayco avatar tteguayco commented on September 28, 2024 1

@sydney-runkle

It should be a new model that doesn't include those fields at all.

I have tried something like:

exclude = [...]   ## An array with the names of those fields to exclude

for field_name in list(new_model.model_fields.keys()):
    if field_name in exclude:
        new_model.model_fields.get(field_name).exclude = True

and even:

exclude = [...]   ## An array with the names of those fields to exclude

for field_name in list(new_model.model_fields.keys()):
    if field_name in exclude:
        new_model.model_fields.pop(field_name)

but when using the newly-created model, the undesired fields are still there, and even populated.

from pydantic.

sydney-runkle avatar sydney-runkle commented on September 28, 2024 1

Yep, gotcha. This reminds me of #6699

from pydantic.

tteguayco avatar tteguayco commented on September 28, 2024 1

Thank you for your answer, @sydney-runkle.

After some back-and-forth attempts, marking the fields as exclude = True makes the new model have them also excluded.

for field_name in list(base_model.model_fields.keys()):
    if  field_name in exclude:
        base_model.model_fields.get(field_name).exclude = True

new_model = create_model('MyNewModel', __base__=base_model)

However, I'm really surprised Pydantic doesn't provide a method to exclude fields in an inherited model out of the box, instead of doing all of this handcrafting work.

from pydantic.

tteguayco avatar tteguayco commented on September 28, 2024 1

Thanks a lot, @sydney-runkle!

from pydantic.

tteguayco avatar tteguayco commented on September 28, 2024

Sorry for the mention, @sydney-runkle.

Do you know if there's a way of doing this? It's a little important for us.

from pydantic.

sydney-runkle avatar sydney-runkle commented on September 28, 2024

@tteguayco,

Do you want to exclude said fields just for serialization, or during validation as well?

from pydantic.

tteguayco avatar tteguayco commented on September 28, 2024

We can mark the above answer as the solution, at least for my specific problem.

from pydantic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.