Skip to content

Commit

Permalink
Fix dictionary size change exception when using SQLAlchemy `associati…
Browse files Browse the repository at this point in the history
…on_proxy` (#624)

* Fix dictionary size change exception when using SQLAlchemy association_proxy

* closes #507
  • Loading branch information
jowilf authored Feb 1, 2025
1 parent a870676 commit 99a1425
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions starlette_admin/contrib/sqla/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(
if self.fields is None or len(self.fields) == 0:
self.fields = [
self.model.__dict__[f].key
for f in self.model.__dict__
for f in list(self.model.__dict__.keys())
if type(self.model.__dict__[f]) is InstrumentedAttribute
]
self.fields = (converter or ModelConverter()).convert_fields_list(
Expand Down Expand Up @@ -134,7 +134,7 @@ def _setup_primary_key(self) -> None:
Tuple[InstrumentedAttribute, ...], InstrumentedAttribute
] = ()
self._pk_coerce: Union[Tuple[type, ...], type] = ()
for key in self.model.__dict__:
for key in list(self.model.__dict__.keys()):
attr = getattr(self.model, key)
if isinstance(attr, InstrumentedAttribute) and getattr(
attr, "primary_key", False
Expand Down
3 changes: 3 additions & 0 deletions tests/sqla/test_sync_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
select,
)
from sqlalchemy.engine import Engine
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import Session, declarative_base, relationship
from sqlalchemy_file.storage import StorageManager
from starlette.applications import Starlette
Expand Down Expand Up @@ -60,6 +61,8 @@ class User(Base):
name = Column(String(100), primary_key=True)
files = Column(sf.FileField(multiple=True))
products = relationship("Product", back_populates="user")
# to reproduce https://github.com/jowilf/starlette-admin/issues/507
product_titles = association_proxy("products", "titles")


class ProductView(ModelView):
Expand Down

0 comments on commit 99a1425

Please sign in to comment.