MongoDB BEanie ODM - Get all Documents linked to a Document

46 Views Asked by At

I have a Many to One relation, no BackLinks, if I have a Document, how, can I get all Documents linked to it?

I tried the following, but I just get an empty list back []:

import asyncio
from models import Summoner, LeagueEntry
from beanie import init_beanie
from motor.motor_asyncio import AsyncIOMotorClient




api_summoner = {
    ...
}

api_league_entries = [
    {
        ...
    },
    {
        ...
    }
]

async def init():
    # Create Motor client
    client = AsyncIOMotorClient()

    # Init beanie with the Product document class
    await init_beanie(database=client.test, document_models=[Summoner, LeagueEntry])


async def main():
    await init()
    
    summoner_1 = Summoner(
        summonerId=api_summoner.pop("id"),
        gameName="Ayato",
        platform="euw1",
        tagLine="11235",
        **api_summoner,
    )
    await summoner_1.insert()

    league_entries = [LeagueEntry(summoner=summoner_1, **entry) for entry in api_league_entries]
    await LeagueEntry.insert_many(league_entries)


    # Find all league entries for the summoner
    my_summoner = await Summoner.find_one(Summoner.gameName == "Ayato")
    print(my_summoner)
    linked_entries = await LeagueEntry.find(LeagueEntry.summoner == my_summoner, fetch_links=True).to_list()
    print(linked_entries)


asyncio.run(main())

models: https://paste.pythondiscord.com/Q2QQ full code: https://paste.pythondiscord.com/76DQ

0

There are 0 best solutions below