For translations I use lazy_gettext in column_labels in a ModelView. With version 1.5.3 this results in an error (was working for version 1.5.2):
TypeError: sequence item 0: expected str instance, _LazyString found
at: libsite-packages\flask_admin\contrib\sqla\view.py", line 608, in search_placeholder
at: return 'Search: %s' % u', '.join(placeholders)
Maybe I am doing translations for column-names wrong and there is another (better) way to provide translations for column-names?
I monkey-patched the method locally to use the the previous statements (i.e. just return gettext('Search')) after I also tried to overwrite the method (which worked but the result is not to my liking, see related issue https://github.com/flask-admin/flask-admin/pull/1728 ):
def search_placeholder(self):
"""
Bug in version 1.5.3: the join on placeholders fails with _LazyString
"""
if not self.column_searchable_list:
return 'Search'
placeholders = []
for searchable in self.column_searchable_list:
if isinstance(searchable, InstrumentedAttribute):
placeholders.append(
str(self.column_labels.get(searchable.key, searchable.key)))
else:
placeholders.append(
str(self.column_labels.get(searchable, searchable)))
return gettext('Search') + ': %s' % u', '.join(placeholders)
I reviewed and merged #1728, see if the latest master fixes the issue for you.
I have not tried the latest master but I do no think it will work since the cause of the exception is not resolved. Below a small test-program I used to reproduce the issue:
from flask_babelex import lazy_gettext
column_searchable_list = ['first_name', 'last_name']
column_labels = {
'first_name': lazy_gettext('First name'),
'last_name': lazy_gettext('Last name')
}
placeholders = []
for searchable in column_searchable_list:
placeholders.append(column_labels.get(searchable, searchable))
try:
search_placeholder = u', '.join(placeholders)
print("Yes: " + search_placeholder)
except Exception:
print("No")
placeholders = []
for searchable in column_searchable_list:
placeholders.append(str(column_labels.get(searchable, searchable)))
try:
search_placeholder = u', '.join(placeholders)
print("Yes: " + search_placeholder)
except Exception:
print("No")
Last master version installed and this bug is still present.
For some Views, removing the column name in a related parent-child table do the trick
# break in 1.5.3, fine in 1.5.2
column_list = ['parent_table.child_table.name']
column_labels = {'parent_table.child_table.name': lazy_gettext('Name')}
fix:
column_list = ['parent_table.child_table']
column_labels = {'parent_table.child_table': lazy_gettext('Name')}
For others views and as mentioned by @fwiersVANAD, a simple
def search_placeholder(self):
return gettext('Search')
is a simple and clean solution
I can confirm that this bug is not fixed on latest release. I was able to work around it by using hints from previous comments and by looking at the current implementation in flask-admin. Just add this to your ModelView.
def search_placeholder(self):
if self.column_searchable_list:
return ", ".join(
str(self.column_labels.get(col, col))
for col in self.column_searchable_list
)
Most helpful comment
Last master version installed and this bug is still present.
For some Views, removing the column name in a related parent-child table do the trick
fix:
For others views and as mentioned by @fwiersVANAD, a simple
is a simple and clean solution