Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Creating Enum class from dictionary #13

Closed
zero0nee opened this issue Jan 26, 2019 · 7 comments
Closed

Creating Enum class from dictionary #13

zero0nee opened this issue Jan 26, 2019 · 7 comments

Comments

@zero0nee
Copy link

I want the user to be able to select parameter values from an enum list of values as a dropdown in the swagger documentation.

The only way, to my knowledge, to do that is to create a Enum pydantic class, and designate a parameter as a part of that class. Which is fine. But in my situation, I want the enum to be dynamically generated based on all unique document values of a property stored in a MongoDB collection. I think you can create a BaseModel class dynamically using the create_model() function, but as far as I know that's not possible with a Enum class.

So my question is: How can I create a parameter enumeration in Swagger from all values in a mongDb collection?

@tiangolo
Copy link
Member

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.

@tiangolo
Copy link
Member

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

@justindujardin
Copy link
Contributor

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'

@saveshodhan
Copy link

saveshodhan commented Sep 13, 2021

@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)

@VianneyMI
Copy link

@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

@myuanz
Copy link

myuanz commented Jul 23, 2022

@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

@github-actions
Copy link
Contributor

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.

@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8328 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

6 participants