3
0
Fork 0

fix: always use prop key for default grid filters

previous logic was using underlying column name, which breaks when the
prop key does not match
This commit is contained in:
Lance Edgar 2025-01-07 15:07:21 -06:00
parent e5f7fe43c2
commit ee8ca11f6a
2 changed files with 23 additions and 12 deletions

View file

@ -42,7 +42,6 @@ dependencies = [
"pyramid_fanstatic", "pyramid_fanstatic",
"pyramid_mako", "pyramid_mako",
"pyramid_tm", "pyramid_tm",
"SQLAlchemy-Utils",
"waitress", "waitress",
"WebHelpers2", "WebHelpers2",
"WuttJamaican[db]>=0.19.2", "WuttJamaican[db]>=0.19.2",

View file

@ -32,7 +32,6 @@ from collections import namedtuple, OrderedDict
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
from sqlalchemy_utils import get_columns
import paginate import paginate
from paginate_sqlalchemy import SqlalchemyOrmPage from paginate_sqlalchemy import SqlalchemyOrmPage
@ -1147,16 +1146,29 @@ class Grid:
filters = filters or {} filters = filters or {}
if self.model_class: if self.model_class:
# nb. i first tried self.get_model_columns() but my notes
# say that was too aggressive in many cases. then i tried # nb. i have found this confusing for some reason. some
# using the *subset* of self.columns, just the ones which # things i've tried so far include:
# corresponded to a property on the model class. and now #
# i am using sa-utils to give the "true" column list.. # i first tried self.get_model_columns() but my notes say
for col in get_columns(self.model_class): # that was too aggressive in many cases.
if col.key in filters: #
continue # then i tried using the *subset* of self.columns, just
prop = getattr(self.model_class, col.key) # the ones which correspond to a property on the model
filters[prop.key] = self.make_filter(prop) # class. but sometimes that skips filters we need.
#
# then i tried get_columns() from sa-utils to give the
# "true" column list, but that fails when the underlying
# column has different name than the prop/attr key.
#
# so now, we are looking directly at the sa mapper, for
# all column attrs and then using the prop key.
inspector = sa.inspect(self.model_class)
for prop in inspector.column_attrs:
if prop.key not in filters:
attr = getattr(self.model_class, prop.key)
filters[prop.key] = self.make_filter(attr)
return filters return filters