fix: prefer attr over key lookup when getting model values
applies to both forms and grids. the base model class can still handle `obj[key]` but now it is limited to the column fields only, no association proxies. so, better to just try `getattr(obj, key)` first and only fall back to the other if it fails. unless the obj is clearly a dict in which case try `obj[key]` only
This commit is contained in:
parent
1d56a4c0d0
commit
0eeeb4bd35
|
@ -1359,12 +1359,15 @@ class Form(object):
|
|||
|
||||
def obtain_value(self, record, field_name):
|
||||
if record:
|
||||
try:
|
||||
|
||||
if isinstance(record, dict):
|
||||
return record[field_name]
|
||||
|
||||
try:
|
||||
return getattr(record, field_name)
|
||||
except AttributeError:
|
||||
pass
|
||||
return record[field_name]
|
||||
except KeyError:
|
||||
return None
|
||||
except TypeError:
|
||||
return getattr(record, field_name, None)
|
||||
|
||||
# TODO: is this always safe to do?
|
||||
elif self.defaults and field_name in self.defaults:
|
||||
|
|
|
@ -586,12 +586,14 @@ class Grid(WuttaGrid):
|
|||
if isinstance(obj, sa.engine.Row):
|
||||
return obj._mapping[column_name]
|
||||
|
||||
try:
|
||||
if isinstance(obj, dict):
|
||||
return obj[column_name]
|
||||
except KeyError:
|
||||
|
||||
try:
|
||||
return getattr(obj, column_name)
|
||||
except AttributeError:
|
||||
pass
|
||||
except TypeError:
|
||||
return getattr(obj, column_name, None)
|
||||
return obj[column_name]
|
||||
|
||||
def render_currency(self, obj, column_name):
|
||||
value = self.obtain_value(obj, column_name)
|
||||
|
|
Loading…
Reference in a new issue