Coder Social home page Coder Social logo

Comments (7)

justindujardin avatar justindujardin commented on May 8, 2024 12

This came up on Google before any pydantic thread, so for anyone else looking it's surprisingly easy:

from pydantic import BaseModel
from enum import Enum
custom_enum_values = {
    "cool": "stuff", 
    "other": "thing",
    "foo": "fighters"
}
TypeEnum = Enum("TypeEnum", custom_enum_values)
class CustomModel(BaseModel):
    type: TypeEnum = TypeEnum.cool

a = CustomModel()
print(a.type.name)  # 'cool'
print(a.type.value)  # 'stuff'

b = CustomModel(type="fighters")
print(f"{b.type.name.capitalize()} {b.type.value.capitalize()}") # 'Foo Fighters'

from fastapi.

saveshodhan avatar saveshodhan commented on May 8, 2024 7

@justindujardin's method works, but in my case it somehow didnt show the enum values in OpenAPI docs schemas.
This SO answer says that's because the class does not inherit from str as well.

As a workaround, I created a temp class that inherits from both, str as well as Enum, and then applied the same technique mentioned by @justindujardin. This then showed up in the docs as well:

class TempEnum(str, Enum):
    pass

TypeEnum = TempEnum("TypeEnum", custom_enum_values)

from fastapi.

tiangolo avatar tiangolo commented on May 8, 2024 1

In theory, you could create an enum dynamically with the functional API: https://docs.python.org/3/library/enum.html#functional-api

And then you could use that with Pydantic dynamic's create_model with that dynamic enum.

I haven't personally tried it yet 😅 .


Nevertheless, if the possible values are "unbounded", if they can be a lot (stored in MongoDB), it might end up being better to use a frontend-side auto-completion widget, with search connected to the API, that then sends the query to MogoDB and keeps filtering the selectable values. Otherwise, you could have a very long select in the front end, that apart from looking ugly could make the browser unresponsive, etc.

from fastapi.

tiangolo avatar tiangolo commented on May 8, 2024

I assume you solved your problem so I'll close this issue now. But feel free to add more comments or create new issues.

from fastapi.

VianneyMI avatar VianneyMI commented on May 8, 2024

@zero0nee @tiangolo
I have a similar use case and implemented something as described in this thread.
Now, my question is how can I make this truly dynamic i.e refresh the dynamic enums as updates are made to my database

from fastapi.

myuanz avatar myuanz commented on May 8, 2024

@zero0nee @tiangolo I have a similar use case and implemented something as described in this thread. Now, my question is how can I make this truly dynamic i.e refresh the dynamic enums as updates are made to my database

You can try this: https://gist.github.com/myuanz/03f3e350fb165ec3697a22b559a7eb50

from fastapi.

github-actions avatar github-actions commented on May 8, 2024

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

from fastapi.

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.