Circular import with beanie ODM
Question:
I need to use a cross-reference in my MongoDB schema. I use beanie as ODM. Here is my models:
entity.py
from beanie import Document
class Entity(Document):
path: List["Folder"] = []
folder.py
from entity import Entity
class Folder(Entity)
pass
init_beanie.py
import beanie
from motor.motor_asyncio import AsyncIOMotorClient
from entity import Entity
from folder import Folder
models = [Entity, Folder]
async def init_beanie():
client = AsyncIOMotorClient("mongo-uri")
Entity.update_forward_refs(Folder=Folder)
await beanie.init_beanie(database=client["mongo-db-name"], document_models=models)
main.py
from fastapi import FastAPI
from init_beanie import init_beanie
my_app = FastAPI()
@my_app.on_event("startup")
async def init():
await init_beanie()
But when I start my app I got an erorr:
...
File "pydantic/main.py", line 816, in pydantic.main.BaseModel.update_forward_refs
File "pydantic/typing.py", line 553, in pydantic.typing.update_model_forward_refs
File "pydantic/typing.py", line 519, in pydantic.typing.update_field_forward_refs
File "pydantic/typing.py", line 65, in pydantic.typing.evaluate_forwardref
File "/usr/local/lib/python3.9/typing.py", line 554, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'Folder' is not defined
what am I doing wrong?
Answers:
To solve this problem you need to modify entity.py
:
entity.py
from typing import TYPE_CHECKING
from beanie import Document
# Need to avoid IDE static type checkings
if TYPE_CHECKING:
from folder import Folder
class Entity(Document):
path: List["Folder"] = []
# Need to be here, AFTER `Entity` class definition
from folder import Folder
# Updating ForwardRefs
Entity.update_forward_refs(Folder=Folder)
In my case update_forward_refs
works only in same module where Entity
class definition placed
I need to use a cross-reference in my MongoDB schema. I use beanie as ODM. Here is my models:
entity.py
from beanie import Document
class Entity(Document):
path: List["Folder"] = []
folder.py
from entity import Entity
class Folder(Entity)
pass
init_beanie.py
import beanie
from motor.motor_asyncio import AsyncIOMotorClient
from entity import Entity
from folder import Folder
models = [Entity, Folder]
async def init_beanie():
client = AsyncIOMotorClient("mongo-uri")
Entity.update_forward_refs(Folder=Folder)
await beanie.init_beanie(database=client["mongo-db-name"], document_models=models)
main.py
from fastapi import FastAPI
from init_beanie import init_beanie
my_app = FastAPI()
@my_app.on_event("startup")
async def init():
await init_beanie()
But when I start my app I got an erorr:
...
File "pydantic/main.py", line 816, in pydantic.main.BaseModel.update_forward_refs
File "pydantic/typing.py", line 553, in pydantic.typing.update_model_forward_refs
File "pydantic/typing.py", line 519, in pydantic.typing.update_field_forward_refs
File "pydantic/typing.py", line 65, in pydantic.typing.evaluate_forwardref
File "/usr/local/lib/python3.9/typing.py", line 554, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'Folder' is not defined
what am I doing wrong?
To solve this problem you need to modify entity.py
:
entity.py
from typing import TYPE_CHECKING
from beanie import Document
# Need to avoid IDE static type checkings
if TYPE_CHECKING:
from folder import Folder
class Entity(Document):
path: List["Folder"] = []
# Need to be here, AFTER `Entity` class definition
from folder import Folder
# Updating ForwardRefs
Entity.update_forward_refs(Folder=Folder)
In my case update_forward_refs
works only in same module where Entity
class definition placed