Skip to content

Enabled prefetched fields#99

Open
artefom wants to merge 1 commit intofastapi-admin:devfrom
artefom:prefetched_fields
Open

Enabled prefetched fields#99
artefom wants to merge 1 commit intofastapi-admin:devfrom
artefom:prefetched_fields

Conversation

@artefom
Copy link

@artefom artefom commented Jan 22, 2022

Enabled prefetching fields from database.

Use case:

Table "tables" has foreign key to "schema" table. And admin pages needs to show schema name in the UI instead of its arbitrary object id.

In this PR, added parsing of 'prefetch' attribute of ComputeFields and these fields are eventually added to obj passed to get_value method.

class ComputedTableSchema(ComputeField):
    prefetch = ('schema__name',)

    async def get_value(self, request: Request, obj: dict):
        return obj["schema__name"]

Another example"

class ComputedColumnTableName(ComputeField):
    prefetch = ('table__name', 'table__schema__name')

    async def get_value(self, request: Request, obj: dict):
        return f'{obj["table__schema__name"]}.{obj["table__name"]}'

@app.register
class ColumnResource(Model):
    label = "Column"
    model = models.Column
    icon = "fas fa-columns"
    page_pre_title = "column list"
    page_title = "column model"
    fields = [
        "id",
        ComputedColumnTableName(name="table_full_name", label="Table"),
        "name",
        "is_primary",
        "comment",
    ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant