Help Needed with FastAPI Dynamic Enum Integration Error #10110
Unanswered
VaishnaviPandey02
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
from enum import Enum
from typing import List
from sqlalchemy.orm import Session
Assuming these functions are defined to get data from the database
def get_customers(db: Session) -> List[str]:
customers = db.query(CoreCustomer.customer_name).all()
return [customer[0] for customer in customers]
def get_providers(db: Session) -> List[str]:
providers = db.query(WasteProvider.provider_name).all()
return [provider[0] for provider in providers]
def get_waste_categories(db: Session) -> List[str]:
waste_categories = db.query(WasteCategory.category_name).all()
return [category[0] for category in waste_categories]
Function to create dynamic Enums
def create_enum(enum_name: str, enum_values: List[str]):
return Enum(enum_name, {value: value for value in enum_values})
Example usage with database session
def setup_dynamic_enums(db: Session):
global CustomerEnum, ProviderEnum, WasteCategoryEnum
@router.post("/my_endpoint/")
async def my_endpoint(
customer: CustomerEnum = Form(...),
provider: ProviderEnum = Form(...),
waste_category: WasteCategoryEnum = Form(...),
pickup_date_column: str = Form(...),
facility_column: str = Form(...),
waste_name_column: str = Form(...),
weight_column: str = Form(...),
is_recycled_column: str = Form(...),
is_diverted_column: str = Form(...),
unit_column: str = Form(...),
file: UploadFile = File(...),
db: Session = Depends(get_db)
):
pass
in main.py
if name == "main":
db_session = get_db()
setup_dynamic_enums(db_session)
How can I dynamically populate a dropdown in a Swagger UI based on a dynamic enum? I am able to achieve this using a static enum.
Beta Was this translation helpful? Give feedback.
All reactions