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 form_widget_args #188

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
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
30 changes: 20 additions & 10 deletions sqladmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand All @@ -225,6 +225,7 @@ async def convert(
prop=prop,
engine=engine,
field_args=field_args,
field_widget_args=field_widget_args,
label=label,
)

Expand Down Expand Up @@ -409,13 +410,15 @@ 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]:
type_name = model.__name__ + "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 {}

Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
)
Expand Down