Replies: 3 comments 7 replies
-
|
Hi @jolmedo 👋 In a typical setup, you should have the following: import motor.motor_asyncio
from beanie import PydanticObjectId
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
DATABASE_URL = "mongodb://localhost:27017"
client = motor.motor_asyncio.AsyncIOMotorClient(
DATABASE_URL, uuidRepresentation="standard"
)
db = client["database_name"]This is the database client Beanie will use. Then, in the startup handler of FastAPI, you should have this: from beanie import init_beanie
@app.on_event("startup")
async def on_startup():
await init_beanie(
database=db, # This is the `db` variable from the snippet above
# Declare here all your models so they are properly initialized
document_models=[
User,
ProductCategory,
ProductSubcategory,
],
)Is it what you have in your project? |
Beta Was this translation helpful? Give feedback.
-
|
Hi @frankie567 ... because you read earlier in the code fragment I've posted that the ProductSubcategory class has a field named category_id, which is linked to ProductCategory, am I right?. So, I'm guessing the order I wrote the Document-based classes in the document_models array is relevant, am I right? While a deeper debugging session through Beanie, I found where the exception So, according to this, there's a protected member named _document_settings which is None and it should be filled during the initialization step, meaning it should happen during the init_beanie call, am I right? That fragment code is at
, started at line 871. My virtual environment Fastapi_users' deployment contains this: At this point, I have to assume one of these things:
My guess? the first one. Issue here is I can't find what I'm doing wrong, or where. Any help, any clarification you can provide will be appreciated. Thanks in advance and best regards. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, @frankie567. Problem solved, my friend. Forget about the files I've already sent you. The key for solving this was given by @roman-right: He asked me for a one-file small project for debugging purposes, meaning he wants to debug whatmy code. I did it myself, and after fixing some module imports, everything works fine. Then, I got back to my project and conceptually, the biggest difference was the import clause: all relative paths were changed to absolute, meaning that instead of
I did
And everything works fine. Maybe, Beanie can't follow relative paths, maybe there's a Pythonic-way to do this (to prefix some path in the relative ones, so Python inspection procedures can follow them, I really don't know), but simply putting the whole path was enough. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Having a well described model and schema using Pydantic and Beanie syntax, there are some collections, represented by their Document classes, which are not been initialized by init_beanie function at the startup event of a Fastapi app.
Did someone know what could be the causes of such a behavior?
Fastapi-users set a very special class named User, which is one of the well initialized by the background Beanie engine. After that, I added my entire model which consists in several classes.
For example, from my product_category module:
From my product_subcategory module:
...and so. The outcome of init_beanie reflects an initialization of a collection named ProductCategory, not product_categories as I think it would happened, because of the Settings inner class with its property "name", and that's it. After several attempts, I really don't know how init_beanie creates proper collections, but it still gives me the same mistake:
beanie.exceptions.CollectionWasNotInitializedSuch a behavior is not documented, and that's why I assume I'm making something wrong. Can anyone know how to fix this?
If I remove all my Document classes, everything works fine. I'm trying to use the connection Fastapi_users set through Beanie to the MongoDB instance for my own purposes. Is it possible? Is there something "behind the scene" that prevents me from doing it?
Thanks in advance. I would appreciate any help you can provide.
Best regards,
Jorge
Beta Was this translation helpful? Give feedback.
All reactions