Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor for computed fields in tables #325

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading