diff --git a/docs/en/docs/reference/resource.md b/docs/en/docs/reference/resource.md index 13029c0..46ef7d4 100644 --- a/docs/en/docs/reference/resource.md +++ b/docs/en/docs/reference/resource.md @@ -26,10 +26,18 @@ General `a` tag menu. `target` attr of `a` tag, default is `_self`. +## Field + +## Action + ## Model -### Field +### get_actions -### Action +### get_bulk_actions + +### row_attributes + +### cell_attributes ## Dropdown diff --git a/examples/resources.py b/examples/resources.py index 22dc4d1..6a33cdc 100644 --- a/examples/resources.py +++ b/examples/resources.py @@ -28,7 +28,10 @@ class AdminResource(Model): page_title = "admin model" filters = [ filters.Search( - name="username", label="Name", search_mode="contains", placeholder="Search for username" + name="username", + label="Name", + search_mode="contains", + placeholder="Search for username", ), filters.Date(name="created_at", label="CreatedAt"), ] @@ -52,6 +55,11 @@ class AdminResource(Model): ] can_create = False + def cell_attributes(self, obj: dict, field: Field) -> dict: + if field.name == "id": + return {"class": "bg-danger"} + return super().cell_attributes(obj, field) + def get_actions(self) -> List[Action]: return [] @@ -111,6 +119,11 @@ class ConfigResource(Model): ), ] + def row_attributes(self, obj: dict) -> dict: + if obj.get("status") == enums.Status.on: + return {"class": "bg-green text-white"} + return super(ConfigResource, self).row_attributes(obj) + @app.register class GithubLink(Link): diff --git a/fastapi_admin/app.py b/fastapi_admin/app.py index b26e824..030c716 100644 --- a/fastapi_admin/app.py +++ b/fastapi_admin/app.py @@ -68,7 +68,8 @@ class FastAPIAdmin(FastAPI): self.resources.append(resource) def get_model_resource(self, model: Type[Model]): - return self.model_resources[model]() + r = self.model_resources.get(model) + return r() if r else None app = FastAPIAdmin( diff --git a/fastapi_admin/depends.py b/fastapi_admin/depends.py index 79610ba..270b782 100644 --- a/fastapi_admin/depends.py +++ b/fastapi_admin/depends.py @@ -21,6 +21,8 @@ def get_model(resource: Optional[str] = Path(...)): def get_model_resource(request: Request, model=Depends(get_model)): model_resource = request.app.get_model_resource(model) # type:Model + if not model_resource: + raise HTTPException(status_code=HTTP_404_NOT_FOUND) actions = model_resource.get_actions() bulk_actions = model_resource.get_bulk_actions() model_resource.actions = actions diff --git a/fastapi_admin/resources.py b/fastapi_admin/resources.py index 94563e9..04de351 100644 --- a/fastapi_admin/resources.py +++ b/fastapi_admin/resources.py @@ -70,6 +70,12 @@ class Model(Resource): can_create: bool = True enctype = "application/x-www-form-urlencoded" + def row_attributes(self, obj: dict) -> dict: + return {} + + def cell_attributes(self, obj: dict, field: Field) -> dict: + return {} + def get_actions(self) -> List[Action]: return [ Action(label=_("update"), icon="ti ti-edit", name="update", ajax=False), @@ -78,7 +84,12 @@ class Model(Resource): def get_bulk_actions(self) -> List[Action]: return [ - Action(label=_("delete_selected"), icon="ti ti-trash", name="delete", method="DELETE"), + Action( + label=_("delete_selected"), + icon="ti ti-trash", + name="delete", + method="DELETE", + ), ] @classmethod diff --git a/fastapi_admin/routes/resources.py b/fastapi_admin/routes/resources.py index 1939e21..7fcb99a 100644 --- a/fastapi_admin/routes/resources.py +++ b/fastapi_admin/routes/resources.py @@ -37,14 +37,15 @@ async def list_view( else: page_size = model_resource.page_size qs = qs.offset((page_num - 1) * page_size) - values = await qs.values_list(*fields_name) - values = await render_values(fields, values) + values = await qs.values(*fields_name) + rendered_values = await render_values(fields, values) context = { "request": request, "resources": resources, "fields_label": fields_label, "fields": fields, "values": values, + "rendered_values": rendered_values, "filters": filters, "resource": resource, "model_resource": model_resource, diff --git a/fastapi_admin/template.py b/fastapi_admin/template.py index 7fac426..095f8cf 100644 --- a/fastapi_admin/template.py +++ b/fastapi_admin/template.py @@ -58,10 +58,10 @@ async def render_values( ret = [] for value in values: item = [] - for i, v in enumerate(value): + for i, k in enumerate(value): if display: - item.append(await fields[i].display.render(v)) + item.append(await fields[i].display.render(value[k])) else: - item.append(await fields[i].input.render(v)) + item.append(await fields[i].input.render(value[k])) ret.append(item) return ret diff --git a/fastapi_admin/templates/list.html b/fastapi_admin/templates/list.html index 2808567..f4f309b 100644 --- a/fastapi_admin/templates/list.html +++ b/fastapi_admin/templates/list.html @@ -107,20 +107,24 @@ - {% for value in values %} - - {% if model_resource.bulk_actions %} - - - - {% endif %} - {% for v in value %} - {{ v|safe }} + {% for value in rendered_values %} + + {% if model_resource.bulk_actions %} + + + + {% endif %} + {% with outer_index = loop.index0 %} + {% for x in value %} + {{ x|safe }} {% endfor %} + {% endwith %} {% if model_resource.actions %}