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

Add custom relationship statements #803

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion sqladmin/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
The converters are from Flask-Admin project.
"""

from __future__ import annotations

import enum
Expand All @@ -23,6 +24,7 @@
RelationshipProperty,
sessionmaker,
)
from sqlalchemy.sql import Select
from sqlalchemy.sql.elements import Label
from sqlalchemy.sql.schema import Column
from wtforms import (
Expand Down Expand Up @@ -109,6 +111,11 @@ def _inner(func: T_CC) -> T_CC:


class ModelConverterBase:
relationships_statements: dict[str, Select] = {}
"""
Select statement for relationships.
"""

_converters: dict[str, ConverterCallable] = {}

def __init__(self) -> None:
Expand Down Expand Up @@ -229,7 +236,10 @@ async def _prepare_select_options(
session_maker: sessionmaker,
) -> list[tuple[str, Any]]:
target_model = prop.mapper.class_
stmt = select(target_model)
if prop.key in self.relationships_statements:
stmt = self.relationships_statements[prop.key]
else:
stmt = select(target_model)

if is_async_session_maker(session_maker):
async with session_maker() as session:
Expand Down
Loading