Skip to content

Commit

Permalink
Add support for sqlalchemy column_property (#408)
Browse files Browse the repository at this point in the history
* Add tests for sqlalchemy column_property

* Fix

* update changelog
  • Loading branch information
jowilf authored Dec 23, 2023
1 parent 37a551c commit 950bf13
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
6 changes: 4 additions & 2 deletions docs/changelog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

* Add Support for SQLAlchemy Models with Multiple Primary Keys by [@jowilf](https://github.com/jowilf)
* Add support for SQLAlchemy `column_property` by [@jowilf](https://github.com/jowilf)
in [#402](https://github.com/jowilf/starlette-admin/pull/402)
* Adds AdminConfig to override app_title, logo_url in the templates
* Add support for SQLAlchemy Models with Multiple Primary Keys by [@jowilf](https://github.com/jowilf)
in [#402](https://github.com/jowilf/starlette-admin/pull/402)
* Adds `AdminConfig` to override `app_title` and `logo_url` in the templates
by [@hasansezertasan](https://github.com/hasansezertasan)
in [#374](https://github.com/jowilf/starlette-admin/pull/374)

Expand Down
9 changes: 8 additions & 1 deletion starlette_admin/contrib/sqla/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Mapper,
RelationshipProperty,
)
from sqlalchemy.sql.elements import ColumnElement, Label
from starlette_admin.contrib.sqla.exceptions import NotSupportedColumn
from starlette_admin.contrib.sqla.fields import FileField, ImageField
from starlette_admin.converters import BaseModelConverter, converts
Expand Down Expand Up @@ -121,8 +122,14 @@ def convert_fields_list(
class ModelConverter(BaseSQLAModelConverter):
@classmethod
def _field_common(
cls, *, name: str, column: Column, **kwargs: Any
cls, *, name: str, column: ColumnElement, **kwargs: Any
) -> Dict[str, Any]:
if isinstance(column, Label):
return {
"name": column.key,
"exclude_from_edit": True,
"exclude_from_create": True,
}
return {
"name": name,
"help_text": column.comment,
Expand Down
4 changes: 3 additions & 1 deletion tests/sqla/test_view_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from sqlalchemy.dialects.mysql import INTEGER, YEAR
from sqlalchemy.dialects.postgresql import BIT, INET, MACADDR, UUID
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy.orm import column_property, declarative_base, relationship
from starlette_admin import (
BooleanField,
DateField,
Expand Down Expand Up @@ -102,6 +102,7 @@ class Other(Base):
year = Column(YEAR)
macaddr = Column(MACADDR)
inet = Column(INET)
cp = column_property(macaddr + ";" + inet)


class UserView(ModelView):
Expand Down Expand Up @@ -197,6 +198,7 @@ def test_other_fields_conversion():
IntegerField("year", min=1901, max=2155),
StringField("macaddr"),
StringField("inet"),
StringField("cp", exclude_from_edit=True, exclude_from_create=True),
]


Expand Down

0 comments on commit 950bf13

Please sign in to comment.