#๐Ÿ”’ MongoDB Beanie BackLinks

4 messages ยท Page 1 of 1 (latest)

deep crest
#

I have this model:

from pydantic import Field

from beanie import Document, Link, BackLink

# A summoner can have multiple league entries or none at all
# A league entry is linked to a summoner
class Summoner(Document):
    game_name: str
    name: str
    platform: str
    profile_icon_id: int
    puuid: str
    summoner_id: str
    summoner_level: int
    tag_line: str

    league_entries: list[BackLink["LeagueEntry"]] = Field(original_field="summoner")

class LeagueEntry(Document):
    league_id: str
    queue_type: str
    tier: str
    rank: str
    league_points: int
    wins: int
    losses: int
    veteran: bool
    inactive: bool
    fresh_blood: bool
    hot_streak: bool

    summoner: Link[Summoner]

If I try to run code like this:

summoner = await Summoner.get("65e8a1b15129e5821bfdd222")
summoner_league_entries = await LeagueEntry.find(LeagueEntry.summoner == summoner).to_list()
print(summoner_league_entries)

But I get a response that BackLink can not be encoded by the encoder

  File "/home/user/projects/project_name/backend/.venv/lib/python3.12/site-packages/beanie/odm/utils/encoder.py", line 138, in encode
    raise ValueError(f"Cannot encode {obj!r}")
ValueError: Cannot encode <beanie.odm.fields.BackLink object at 0x7f3263d49010>

Am I using BackLinks wrong?

rustic rampartBOT
#

@deep crest

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rustic rampartBOT
#

@deep crest

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.