Skip to content

Commit

Permalink
Fix column_property in forms (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee committed Jul 12, 2024
1 parent b0ae55a commit 8acef5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sqladmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import anyio
from sqlalchemy import Boolean, select
from sqlalchemy import inspect as sqlalchemy_inspect
from sqlalchemy.orm import ColumnProperty, RelationshipProperty, sessionmaker
from sqlalchemy.orm import (
ColumnProperty,
RelationshipProperty,
sessionmaker,
)
from sqlalchemy.sql.elements import Label
from sqlalchemy.sql.schema import Column
from wtforms import (
BooleanField,
Expand Down Expand Up @@ -612,9 +617,12 @@ async def get_model_form(
attributes = []
names = only or mapper.attrs.keys()
for name in names:
if exclude and name in exclude:
attr = mapper.attrs[name]
if (exclude and name in exclude) or (
isinstance(attr, ColumnProperty) and isinstance(attr.expression, Label)
):
continue
attributes.append((name, mapper.attrs[name]))
attributes.append((name, attr))

field_dict = {}
for name, attr in attributes:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_forms/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
Text,
Time,
TypeDecorator,
func,
select,
)
from sqlalchemy.dialects.postgresql import ARRAY, INET, MACADDR, UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import (
ColumnProperty,
column_property,
composite,
declarative_base,
relationship,
Expand Down Expand Up @@ -333,3 +336,16 @@ class DataModel(Base):
assert inspect.isfunction(Form.process)
assert inspect.isfunction(Form.validate)
assert inspect.isfunction(Form.populate_obj)


async def test_column_property_is_ignored_in_form() -> None:
class Model(Base):
__tablename__ = "model_column_property"

id = Column(Integer, primary_key=True)
number = Column(Integer)
count = column_property(select(func.count("Model")).scalar_subquery())

Form = await get_model_form(model=Model, session_maker=session_maker)

assert "count" not in Form()._fields

0 comments on commit 8acef5b

Please sign in to comment.