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

Fix column_property #791

Merged
merged 1 commit into from
Jul 12, 2024
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
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
Loading