循环导入与 Beanie ODM

huangapple go评论61阅读模式
英文:

Circular import with beanie ODM

问题

我需要在我的MongoDB模式中使用交叉引用。我使用beanie作为ODM。以下是我的模型:

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()

但是当我启动我的应用程序时,我收到了一个错误:

  ...
  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

我做错了什么?

英文:

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?

答案1

得分: 1

为了解决这个问题,您需要修改entity.py文件:

entity.py

from typing import TYPE_CHECKING
from beanie import Document

# 需要避免IDE静态类型检查
if TYPE_CHECKING:
    from folder import Folder

class Entity(Document):
    path: List["Folder"] = []

# 需要在`Entity`类定义之后放置
from folder import Folder

# 更新ForwardRefs
Entity.update_forward_refs(Folder=Folder)
英文:

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

huangapple
  • 本文由 发表于 2023年2月19日 09:09:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定