Backling to multiple documents #746
mike-pisman
started this conversation in
Feature Request
Replies: 1 comment
-
As a possible workaround, I tried to use inheritance creating a Members root document, and derive 2 documents for each type of BackLink. But that does not work, as when printing out the Documents with fetched links, import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import Document, Link, WriteRules, init_beanie, BackLink
from pydantic import Field
class Organization(Document):
name: str
teams: list[Link["Team"]] = []
members: list[Link["OrgMember"] | Link["TeamMember"]] = []
class Team(Document):
name: str
org: BackLink[Organization] = Field(original_field="organization")
members: list[Link["OrgMember"] | Link["TeamMember"]] = []
class Account(Document):
name: str
class Member(Document):
account: Link[Account]
class Settings:
is_root = True
class OrgMember(Member):
resource: BackLink[Organization] = Field(original_field="members")
class TeamMember(Member):
resource: BackLink[Team] = Field(original_field="members")
async def get_backlink(member: OrgMember | TeamMember):
await member.fetch_all_links()
print(member)
async def example():
client = AsyncIOMotorClient("mongodb://***:27017")
await init_beanie(database=client.test,
document_models=[Organization, Team, Member, Account, OrgMember, TeamMember])
# Create documents
account = await Account(name="John Smith").create() # Create an account
org = await Organization(name="Beanie").create() # Create an organization
team = await Team(name="Developers").create() # Create a team
org_member = await OrgMember(account=account).create()
team_member = await Member(account=account).create()
# Add team to organization
org.teams.append(team)
await org.save(link_rule=WriteRules.WRITE)
# Add member to organization
org.members.append(org_member)
await org.save(link_rule=WriteRules.WRITE)
# Add member to team
team.members.append(team_member)
await team.save(link_rule=WriteRules.WRITE)
members = await Member.find({},
fetch_links=True).to_list()
for member in members:
print(member)
await get_backlink(member)
if __name__ == "__main__":
asyncio.run(example()) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Would it be possible to use
BackLink
with multiple documents?For example, let's say there are 3 documents: organizations, teams, and accounts. Organizations can have multiple members - list of links to accounts, and they also can have teams(which represent groups of members) - a separate document which also contains list of links to accounts. Since we cannot use circular linking, would it be possible to use BackLink with multiple documents, provided the
original_field
is the same in both documents?Alternatively, would it be possible to have only one BackLink document, but have it of two type? In the diagram bellow you can see that
organization:members
link tomember
document, while theteams:members
link to a differentmember
document; However, both member documents link to the sameaccount
document. This way,member
can BackLink to only one document,but that document can be of two types...
Beta Was this translation helpful? Give feedback.
All reactions