From 564b10e0ef596174cec53db6bb6f7ddb32069ae2 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 19 Jun 2022 17:05:48 +0200 Subject: [PATCH 1/2] Add form_widget_args --- sqladmin/forms.py | 28 +++++++++++++++++++--------- sqladmin/models.py | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/sqladmin/forms.py b/sqladmin/forms.py index 35366326..d8c4e6b3 100644 --- a/sqladmin/forms.py +++ b/sqladmin/forms.py @@ -93,20 +93,20 @@ async def _prepare_kwargs( self, prop: Union[ColumnProperty, RelationshipProperty], engine: Union[Engine, AsyncEngine], - field_args: Dict[str, Any] = None, + field_args: Dict[str, Any], + field_widget_args: Dict[str, Any], label: Optional[str] = None, ) -> Optional[Dict[str, Any]]: - if field_args: - kwargs = field_args.copy() - else: - kwargs = {} + kwargs = field_args.copy() + widget_args = field_widget_args.copy() + widget_args.setdefault("class", "form-control") kwargs.setdefault("label", label) kwargs.setdefault("validators", []) kwargs.setdefault("filters", []) kwargs.setdefault("default", None) kwargs.setdefault("description", prop.doc) - kwargs.setdefault("render_kw", {"class": "form-control"}) + kwargs.setdefault("render_kw", widget_args) column = None @@ -213,10 +213,10 @@ def get_converter( async def convert( self, model: type, - mapper: Mapper, prop: Union[ColumnProperty, RelationshipProperty], engine: Union[Engine, AsyncEngine], - field_args: Dict[str, Any] = None, + field_args: Dict[str, Any], + field_widget_args: Dict[str, Any], label: Optional[str] = None, override: Optional[Type[Field]] = None, ) -> Optional[UnboundField]: @@ -225,6 +225,7 @@ async def convert( prop=prop, engine=engine, field_args=field_args, + field_widget_args=field_widget_args, label=label, ) @@ -409,6 +410,7 @@ async def get_model_form( exclude: Sequence[str] = None, column_labels: Dict[str, str] = None, form_args: Dict[str, Dict[str, Any]] = None, + form_widget_args: Dict[str, Dict[str, Any]] = None, form_class: Type[Form] = Form, form_overrides: Dict[str, Dict[str, Type[Field]]] = None, ) -> Type[Form]: @@ -416,6 +418,7 @@ async def get_model_form( converter = ModelConverter() mapper = sqlalchemy_inspect(model) form_args = form_args or {} + form_widget_args = form_widget_args or {} column_labels = column_labels or {} form_overrides = form_overrides or {} @@ -429,10 +432,17 @@ async def get_model_form( field_dict = {} for name, attr in attributes: field_args = form_args.get(name, {}) + field_widget_args = form_widget_args.get(name, {}) label = column_labels.get(name, None) override = form_overrides.get(name, None) field = await converter.convert( - model, mapper, attr, engine, field_args, label, override + model=model, + prop=attr, + engine=engine, + field_args=field_args, + field_widget_args=field_widget_args, + label=label, + override=override, ) if field is not None: field_dict[name] = field diff --git a/sqladmin/models.py b/sqladmin/models.py index 5971bd92..253ebfce 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -431,6 +431,21 @@ class MyModelAdmin(ModelAdmin, model=User): ``` """ + form_widget_args: ClassVar[Dict[str, Dict[str, Any]]] = {} + """Dictionary of form widget rendering arguments. + Use this to customize how widget is rendered without using custom template. + + ???+ example + ```python + class UserAdmin(ModelAdmin, model=User): + form_widget_args = { + "email": { + "readonly": True, + }, + } + ``` + """ + form_columns: ClassVar[Sequence[Union[str, InstrumentedAttribute]]] = [] """List of columns to include in the form. Columns can either be string names or SQLAlchemy columns. @@ -842,6 +857,7 @@ async def scaffold_form(self) -> Type[Form]: only=[i[1].key for i in self._form_attrs], column_labels={k.key: v for k, v in self._column_labels.items()}, form_args=self.form_args, + form_widget_args=self.form_widget_args, form_class=self.form_base_class, form_overrides=self.form_overrides, ) From 6549409897692195a6b32eb65770af245e8d3377 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Mon, 20 Jun 2022 09:27:44 +0200 Subject: [PATCH 2/2] fix lint --- sqladmin/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqladmin/forms.py b/sqladmin/forms.py index d8c4e6b3..06721b9a 100644 --- a/sqladmin/forms.py +++ b/sqladmin/forms.py @@ -18,7 +18,7 @@ from sqlalchemy import inspect as sqlalchemy_inspect, select from sqlalchemy.engine import Engine from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession -from sqlalchemy.orm import ColumnProperty, Mapper, RelationshipProperty, Session +from sqlalchemy.orm import ColumnProperty, RelationshipProperty, Session from sqlalchemy.sql.schema import Column from typing_extensions import Protocol from wtforms import (