Skip to content

Commit 61e0c0d

Browse files
Add some field props from SQLAlchemy models (#764)
1 parent d8ab462 commit 61e0c0d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

aiohttp_admin/backends/sqlalchemy.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,24 @@ def __init__(self, db: AsyncEngine, model_or_table: Union[sa.Table, type[Declara
177177
"target": key.column.name}
178178
else:
179179
field, inp, props = get_components(c.type)
180-
props["source"] = c.name
181180

181+
props["source"] = c.name
182182
if isinstance(c.type, sa.Enum):
183183
props["choices"] = tuple({"id": e.value, "name": e.name}
184184
for e in c.type.python_type)
185185

186+
length = getattr(c.type, "length", 0)
187+
if length is None or length > 31:
188+
props["fullWidth"] = True
189+
if length is None or length > 127:
190+
props["multiline"] = True
191+
186192
if isinstance(c.default, sa.ColumnDefault):
187193
props["placeholder"] = c.default.arg
188194

195+
if c.comment:
196+
props["helperText"] = c.comment
197+
189198
self.fields[c.name] = comp(field, props)
190199
if c.computed is None:
191200
# TODO: Allow custom props (e.g. disabled, multiline, rows etc.)

tests/test_backends_sqlalchemy.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ class TestModel(base): # type: ignore[misc,valid-type]
3535
assert r.name == "dummy"
3636
assert r.primary_key == "id"
3737
assert r.fields == {"id": comp("NumberField", {"source": "id"}),
38-
"num": comp("TextField", {"source": "num"})}
38+
"num": comp("TextField", {"source": "num", "fullWidth": True,
39+
"multiline": True})}
3940
# Autoincremented PK should not be in create form
4041
assert r.inputs == {
4142
"id": comp("NumberInput", {"source": "id", "validate": [func("required", ())]})
4243
| {"show_create": False},
43-
"num": comp("TextInput", {"source": "num", "validate": [func("required", ())]})
44+
"num": comp("TextInput", {
45+
"source": "num", "fullWidth": True, "multiline": True,
46+
"validate": [func("required", ())]})
4447
| {"show_create": True}
4548
}
4649

@@ -66,6 +69,21 @@ def test_table(mock_engine: AsyncEngine) -> None:
6669
}
6770

6871

72+
def test_extra_props(base: type[DeclarativeBase], mock_engine: AsyncEngine) -> None:
73+
class TestModel(base): # type: ignore[misc,valid-type]
74+
__tablename__ = "dummy"
75+
id: Mapped[int] = mapped_column(primary_key=True)
76+
num: Mapped[str] = mapped_column(sa.String(128), comment="Foo", default="Bar")
77+
78+
r = SAResource(mock_engine, TestModel)
79+
assert r.fields["num"]["props"] == {
80+
"source": "num", "fullWidth": True, "multiline": True, "placeholder": "Bar",
81+
"helperText": "Foo"}
82+
assert r.inputs["num"]["props"] == {
83+
"source": "num", "fullWidth": True, "multiline": True, "placeholder": "Bar",
84+
"helperText": "Foo", "validate": [func("maxLength", (128,))]}
85+
86+
6987
async def test_binary(
7088
base: DeclarativeBase, aiohttp_client: Callable[[web.Application], Awaitable[TestClient]],
7189
login: _Login
@@ -243,19 +261,21 @@ async def test_nonid_pk(base: type[DeclarativeBase], mock_engine: AsyncEngine) -
243261
class TestModel(base): # type: ignore[misc,valid-type]
244262
__tablename__ = "test"
245263
num: Mapped[int] = mapped_column(primary_key=True)
246-
other: Mapped[str]
264+
other: Mapped[str] = mapped_column(sa.String(64))
247265

248266
r = SAResource(mock_engine, TestModel)
249267
assert r.name == "test"
250268
assert r.primary_key == "num"
251269
assert r.fields == {
252270
"num": comp("NumberField", {"source": "num"}),
253-
"other": comp("TextField", {"source": "other"})
271+
"other": comp("TextField", {"source": "other", "fullWidth": True})
254272
}
255273
assert r.inputs == {
256274
"num": comp("NumberInput", {"source": "num", "validate": [func("required", ())]})
257275
| {"show_create": False},
258-
"other": comp("TextInput", {"source": "other", "validate": [func("required", ())]})
276+
"other": comp("TextInput", {
277+
"fullWidth": True, "source": "other",
278+
"validate": [func("required", ()), func("maxLength", (64,))]})
259279
| {"show_create": True}
260280
}
261281

0 commit comments

Comments
 (0)