From eb7deba15940db73aee88f4c676e47f4650e799d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 15 Aug 2024 10:29:48 +0300 Subject: [PATCH 1/4] Add custom relationship statements --- sqladmin/forms.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sqladmin/forms.py b/sqladmin/forms.py index 00daa7d1..a9f5c770 100644 --- a/sqladmin/forms.py +++ b/sqladmin/forms.py @@ -109,6 +109,11 @@ def _inner(func: T_CC) -> T_CC: class ModelConverterBase: + relationships_statements: dict[str, str] = {} + """ + Select statement for relationships. + """ + _converters: dict[str, ConverterCallable] = {} def __init__(self) -> None: @@ -229,7 +234,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: From 315f41e461bd7bf19a04a64cd54cb2f223923fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 15 Aug 2024 10:45:29 +0300 Subject: [PATCH 2/4] Fix type hint --- sqladmin/forms.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sqladmin/forms.py b/sqladmin/forms.py index a9f5c770..9e3d60db 100644 --- a/sqladmin/forms.py +++ b/sqladmin/forms.py @@ -1,6 +1,7 @@ """ The converters are from Flask-Admin project. """ + from __future__ import annotations import enum @@ -16,7 +17,11 @@ ) import anyio -from sqlalchemy import Boolean, select +from sqlalchemy import ( + Boolean, + Select, + select, +) from sqlalchemy import inspect as sqlalchemy_inspect from sqlalchemy.orm import ( ColumnProperty, @@ -109,7 +114,7 @@ def _inner(func: T_CC) -> T_CC: class ModelConverterBase: - relationships_statements: dict[str, str] = {} + relationships_statements: dict[str, Select] = {} """ Select statement for relationships. """ From 32774d2cf26bfa7c29f80986b1d49976ea4ea42f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 15 Aug 2024 10:54:50 +0300 Subject: [PATCH 3/4] Fix hinting for sqlalchemy==1.4 --- sqladmin/forms.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sqladmin/forms.py b/sqladmin/forms.py index 9e3d60db..89e78ad0 100644 --- a/sqladmin/forms.py +++ b/sqladmin/forms.py @@ -17,11 +17,7 @@ ) import anyio -from sqlalchemy import ( - Boolean, - Select, - select, -) +from sqlalchemy import Boolean, select from sqlalchemy import inspect as sqlalchemy_inspect from sqlalchemy.orm import ( ColumnProperty, @@ -114,7 +110,7 @@ def _inner(func: T_CC) -> T_CC: class ModelConverterBase: - relationships_statements: dict[str, Select] = {} + relationships_statements: dict[str, Any] = {} """ Select statement for relationships. """ From aedcbbb962edc3376c3c2900b31a54ea0aa5126d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 15 Aug 2024 10:59:44 +0300 Subject: [PATCH 4/4] Fix hinting for sqlalchemy==1.4 --- sqladmin/forms.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqladmin/forms.py b/sqladmin/forms.py index 89e78ad0..f59d33ee 100644 --- a/sqladmin/forms.py +++ b/sqladmin/forms.py @@ -24,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 ( @@ -110,7 +111,7 @@ def _inner(func: T_CC) -> T_CC: class ModelConverterBase: - relationships_statements: dict[str, Any] = {} + relationships_statements: dict[str, Select] = {} """ Select statement for relationships. """