Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling references to external models by id #167

Open
bbalet opened this issue Jul 31, 2021 · 1 comment
Open

Handling references to external models by id #167

bbalet opened this issue Jul 31, 2021 · 1 comment
Labels
enhancement New feature or request reference-rework

Comments

@bbalet
Copy link

bbalet commented Jul 31, 2021

Hi,

I'm working on a FastAPI application with ODMantic and it's fantastic. However, I'm a bit confused by how references to external models are handled. Maybe I did it all wrong, but I came up with the same solution than #127 (comment) in order to avoid a validation error

Let's say that I'm trying to model a project and its builds:

class Build(Model):
    project: Project = Reference()
    name: str
    date: datetime
    success: bool

I must use an additional model:

class BuildInApi(Model):
    project_id: str
    name: str
    date: datetime
    success: bool

Or project_id: Optional[ObjectId] but it's the same.

And then to:

  1. Find the related project.
  2. Map the fields between the two models

As in this example endpoint:

@router.put("/builds/", response_model=Build, tags=["builds"])
async def create_build(build_in_api: BuildInApi, request: Request):
    project = await request.app.engine.find_one(Project, Project.id == ObjectId(build_in_api.project_id))
    build = Build(project=project, 
                    name=build_in_api.name, 
                    date=build_in_api.date, 
                    success=build_in_api.success, 
                    payload=build_in_api.payload)
    await request.app.engine.save(build)
    return build

This is cumbersome and errorprone so I'm sure I am missing something.

@art049
Copy link
Owner

art049 commented Sep 25, 2022

Hello, thanks for showing this use case. Yeah, currently this is, unfortunately, the only way.
Although you could avoid manual unpacking like this:

@router.put("/builds/", response_model=Build, tags=["builds"])
async def create_build(build_in_api: BuildInApi, request: Request):
    project = await request.app.engine.find_one(Project, Project.id == ObjectId(build_in_api.project_id))
    build = Build(project=project, **build_in_api.dict(exclude="project_id"))
    await request.app.engine.save(build)
    return build

However, it doesn't relieve the pain that much.
I'll include this use case in the reference rework coming soon!

@art049 art049 changed the title [QUESTION] How to Handle references to external models? Handling references to external models by id Sep 25, 2022
@art049 art049 added the enhancement New feature or request label Sep 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request reference-rework
Projects
None yet
Development

No branches or pull requests

2 participants