Nested Routes#

A nested resource is a child resource that the URL places under its parent, such as /projects/{project_id}/tasks. In Restly, it is a normal AsyncRestView with three additions: a path parameter in its prefix, a dependency that looks up the parent, and a scope that limits the rows to that parent. Every CRUD route keeps working, including pages, filters and sorting.

A flat view that filters on the foreign key, GET /tasks?project_id=17, needs no extra code; see Filter by a foreign key. Nest the view when the nested URL is part of your API.

Nest one level#

This example serves the tasks of a project at /projects/{project_id}/tasks:

import sqlalchemy as sa
from fastapi import FastAPI
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

import fastapi_restly as fr


class Base(fr.ModelDefaults, DeclarativeBase):
    pass


class Project(fr.IDMixin, Base):
    name: Mapped[str]
    archived: Mapped[bool] = mapped_column(default=False)


class Task(fr.IDMixin, Base):
    title: Mapped[str]
    project_id: Mapped[int] = mapped_column(sa.ForeignKey("project.id"), init=False)


class ProjectSchema(fr.IDSchema):
    name: str


class TaskSchema(fr.IDSchema):
    title: str
    project_id: fr.ReadOnly[int]


class Parent(fr.ContextNamespace):
    project_id: fr.ContextParam[int]


app = FastAPI()
fr.configure(app, async_database_url="sqlite+aiosqlite:///app.db")


@fr.include_view(app)
class ProjectView(fr.AsyncRestView):
    prefix = "/projects"
    model = Project
    schema = ProjectSchema
    scope = fr.where_clause(Project.archived.is_(False))


async def project_from_path(project_id: int, session: fr.AsyncSessionDep) -> int:
    stmt = fr.apply_clauses(
        sa.select(Project.id).where(Project.id == project_id),
        fr.resolve_scope(ProjectView),
    )
    found = await session.scalar(stmt)
    if found is None:
        raise fr.exc.NotFound(f"Project with id {project_id} was not found")
    return found


@fr.include_view(app)
class ProjectTaskView(fr.AsyncRestView):
    prefix = "/projects/{project_id}/tasks"
    model = Task
    schema = TaskSchema
    dependencies = [Parent.depends(project_id=project_from_path)]
    scope = fr.all_of(
        fr.resolve_scope(Task),
        fr.where_clause(Task.project_id == Parent.project_id),
    )

    async def create(self, data):
        task = await super().create(data)
        task.project_id = Parent.project_id()
        return task

ProjectTaskView differs from a flat view in these places:

  • prefix contains {project_id}, so every route of the view is under a project. Name the path parameter after the parent, as here. The item routes already use {id}, and FastAPI raises an error for a second {id} when you register the view.

  • project_from_path reads project_id from the path. FastAPI checks that it is an integer, and OpenAPI lists it on every route. The function returns 404 when the project does not exist or when ProjectView hides it.

  • Parent.depends binds the project id for the request. Parent is a fr.ContextNamespace, like Current in Request Context. One Parent serves every nested view in the app.

  • scope limits every read to the tasks of that project: the list, its total count, get one, update and delete. A task of another project returns 404. A view’s scope replaces the model’s default scope (Give one view other rows), so fr.resolve_scope(Task) adds the default scope back.

  • create sets the project id; see Create a child.

The routes then answer like this:

GET    /projects/1/tasks?sort=-title   the tasks of project 1, by title, descending
POST   /projects/1/tasks               a new task in project 1
GET    /projects/2/tasks/7             404 when task 7 is in another project
GET    /projects/99/tasks              404 when project 99 does not exist
GET    /projects/x/tasks               422, x is not an integer

Details

A sync fr.RestView works in the same way. Write the lookups as plain functions that take fr.SessionDep, and write the create override as a plain def without await.

Create a child#

The project id comes from the path, not from the request body. TaskSchema marks project_id as fr.ReadOnly, so the generated create and update schemas leave it out. The create override sets it from Parent after super().create() builds the task. The INSERT has the id, because Restly saves after create returns.

Declare the foreign key with init=False. super().create() builds the task from the request body, which has no project_id. Without init=False, the model requires project_id in its constructor, and the create returns 500.

Keep the parent id out of every write schema of the nested view. If TaskSchema declared a writable project_id, the generated update schema would include it. An explicit write schema writes every field it declares. In both cases, a PATCH request could move the task to a project that the URL never named, and the lookup would not check it.

Details

Set the parent id in the view, not in a column default that reads Parent.project_id. Only the nested route knows the parent, so the value belongs in create; see Values every write path sets for the difference. With such a default, creating a task anywhere else fails, for example in a flat view or a script, because nothing binds Parent there.

Which parents a client can reach#

The lookup decides which parents a client can reach. project_from_path applies the scope of ProjectView through fr.resolve_scope. So a project that GET /projects/{id} hides also returns 404 here, with all of its tasks. In the example, that is an archived project.

Without this check, POST /projects/{project_id}/tasks creates a task in any project whose id a client can guess.

The lookup does not run the authorize method of ProjectView. Put rules about which rows a user may see in a scope, so that both views apply them. The nested view still runs its own authorize on each of its routes.

Nest more than one level#

For more levels, build a chain of base views, one for each level. Each base adds its segment to prefix and its lookup to dependencies. Both add up from base to subclass; see Concatenate URL prefixes. This example serves tasks at /companies/{company_id}/projects/{project_id}/tasks. It is a separate app. It uses the imports, app, Base, Task and TaskSchema of the first example, and its own Project, Parent and lookups:

from typing import Annotated

from fastapi import Depends


class Company(fr.IDMixin, Base):
    name: Mapped[str]


class Project(fr.IDMixin, Base):
    name: Mapped[str]
    company_id: Mapped[int] = mapped_column(sa.ForeignKey("company.id"), init=False)


class Parent(fr.ContextNamespace):
    company_id: fr.ContextParam[int]
    project_id: fr.ContextParam[int]


async def company_from_path(company_id: int, session: fr.AsyncSessionDep) -> int:
    stmt = fr.apply_clauses(
        sa.select(Company.id).where(Company.id == company_id),
        fr.resolve_scope(Company),
    )
    found = await session.scalar(stmt)
    if found is None:
        raise fr.exc.NotFound(f"Company with id {company_id} was not found")
    return found


async def project_from_path(
    project_id: int,
    company_id: Annotated[int, Depends(company_from_path)],
    session: fr.AsyncSessionDep,
) -> int:
    stmt = fr.apply_clauses(
        sa.select(Project.id).where(
            Project.id == project_id, Project.company_id == company_id
        ),
        fr.resolve_scope(Project),
    )
    found = await session.scalar(stmt)
    if found is None:
        raise fr.exc.NotFound(f"Project with id {project_id} was not found")
    return found


class CompanyScoped(fr.AsyncRestView):
    prefix = "/companies/{company_id}"
    dependencies = [Parent.depends(company_id=company_from_path)]


class ProjectScoped(CompanyScoped):
    prefix = "/projects/{project_id}"
    dependencies = [Parent.depends(project_id=project_from_path)]


@fr.include_view(app)
class ProjectTaskView(ProjectScoped):
    prefix = "/tasks"
    model = Task
    schema = TaskSchema
    scope = fr.all_of(
        fr.resolve_scope(Task),
        fr.where_clause(Task.project_id == Parent.project_id),
    )

    async def create(self, data):
        task = await super().create(data)
        task.project_id = Parent.project_id()
        return task

project_from_path gets the company id from company_from_path as a FastAPI sub-dependency. FastAPI runs the company lookup first, once per request, and returns 404 for an unknown company. The project query also checks that the project belongs to that company. So /companies/2/projects/1/tasks returns 404 when project 1 is in company 1.

Each lookup applies the scope of its parent, as in Which parents a client can reach. This example has no views for companies or projects, so it passes the models. When a view serves the parent, such as a ProjectView, pass the view instead. The nested routes then hide what that view hides.

Details

Do not read Parent.company_id() in project_from_path instead. When the company id in the path is not an integer, FastAPI still runs the project lookup. Parent.company_id is then not bound, and the request returns 500 instead of 422.

Let each base inherit the one above it, as ProjectScoped inherits CompanyScoped. Two mixins side by side, class ProjectTaskView(CompanyMixin, ProjectMixin, fr.AsyncRestView), add their segments in reverse order: /projects/{project_id}/companies/{company_id}/tasks.

Each level adds one query to every request.

A flat view next to the nested one#

A model can have a flat view and nested views at the same time, such as a TaskView at /tasks next to ProjectTaskView. Watch these points:

  • Each view applies its own scope. Rows that the scope of TaskView hides, such as the tasks of other users, still show under the nested URL. To hide them there too, use fr.resolve_scope(TaskView) instead of fr.resolve_scope(Task) in the scope of ProjectTaskView.

  • TaskView cannot create tasks with TaskSchema, because project_id is read-only there. Give TaskView its own create_schema, a fr.BaseSchema with project_id: fr.MustExist[int, Project]. Keep project_id read-only in TaskSchema; see Create a child.

  • Both views appear under the “Tasks” tag in OpenAPI, unless you set tags.