Skip to content

Commit

Permalink
Minor refactor for computed fields in tables (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle authored May 29, 2024
1 parent 7c921b0 commit 61d9b0c
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/python-fastui/fastui/components/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,17 @@ def _fill_columns(self) -> _te.Self:
except IndexError:
raise ValueError('Cannot infer model from empty data, please set `Table(..., model=MyModel)`')

all_model_fields = {**data_model_type.model_fields, **data_model_type.model_computed_fields}
if self.columns is None:
self.columns = []
for name, field in data_model_type.model_fields.items():
self.columns.append(display.DisplayLookup(field=name, title=field.title))
for name, field in data_model_type.model_computed_fields.items():
self.columns.append(display.DisplayLookup(field=name, title=field.title))

self.columns = [
display.DisplayLookup(field=name, title=field.title) for name, field in all_model_fields.items()
]
else:
# add pydantic titles to columns that don't have them
for column in (c for c in self.columns if c.title is None):
model_field = data_model_type.model_fields.get(column.field)
computed_field = data_model_type.model_computed_fields.get(column.field)
if model_field and model_field.title:
column.title = model_field.title
elif computed_field and computed_field.title:
column.title = computed_field.title
field = all_model_fields.get(column.field)
if field and field.title:
column.title = field.title
return self

@classmethod
Expand Down

0 comments on commit 61d9b0c

Please sign in to comment.