[Python] beanie.exceptions.CollectionWasNotInitialized error

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new to the Beanie library which is


    an asynchronous Python object-document mapper (ODM) for MongoDB. Data models are based on Pydantic.
    ​

    I was trying this library with fastAPI framework, and made an ODM for some document, let's say it's name is SomeClass and then tried to insert some data in the db using this ODM.
    Here's the code for ODM and the method to create a document (insomeClass.py):

    from beanie import Document
    from pydantic import Field, BaseModel

    class SomeClassDto(BaseModel):
    """
    A Class for Data Transferring.
    """
    name: str = Field(max_length=maxsize, min_length=1)


    class SomeClassDao:
    """
    This is a class which holds the 'SomeClass' class (inherited from Beanie Document),
    and also, the methods which use the 'SomeClass' class.
    """
    class SomeClass(Document):
    name: str = Field(max_length=20, min_length=1)


    @classmethod
    async def create_some_class(cls, body: SomeClassDto):
    some_class = cls.SomeClass(**body.dict())
    return await cls.SomeClass.insert_one(some_class)


    I've used and called the create_some_class function, but it throwed this error:
    beanie.exceptions.CollectionWasNotInitialized

    However the error is self-explanatory but I didn't understand at first, and couldn't find any relatable question about my problem in SO, so I decided to post this question and answer it, for the sake of future.

    Continue reading...

Compartilhe esta Página