Skip to content

Is it possible to have a dynamic collection name? So that I can use the same Schema for multiple collections? #849

Answered by dotKokott
dotKokott asked this question in Question
Discussion options

You must be logged in to vote

Found a solution by now. Feels like a workaround but it works

class DatasetItem(Document):
  pass

async def get_dataset_item_model(
    db_name: str,
    collection_name: str,
) -> type[DatasetItem]:
    class DynamicDatasetItem(DatasetItem):
        class Settings:
            name = collection_name

    init_beanie(db=db_name, document_models=[DynamicDatasetItem])

    return DynamicDatasetItem

model1 = await get_dataset_item_model("DB1", "Collection1")
model2 = await get_dataset_item_model("DB2", "Collection2")

await model2.get(...)

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by dotKokott
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants