diff --git a/CHANGELOG.md b/CHANGELOG.md index b99ecc8..d79e6b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ## 1.0 -### 0.1.4 +### 1.0.4 + +- Add `widgets.filters.Boolean` class ### 1.0.3 diff --git a/fastapi_admin/widgets/filters.py b/fastapi_admin/widgets/filters.py index 56cb544..d580392 100644 --- a/fastapi_admin/widgets/filters.py +++ b/fastapi_admin/widgets/filters.py @@ -1,6 +1,6 @@ import abc from enum import Enum as EnumCLS -from typing import Any, Optional, Tuple, Type +from typing import Any, List, Optional, Tuple, Type import pendulum from starlette.requests import Request @@ -191,3 +191,21 @@ class DistinctColumn(Select): async def get_values(self): return await self.model.all().distinct().values_list(self.name) + + +class Boolean(Select): + async def get_options(self) -> List[Tuple[str, str]]: + """Return list of possible values to select from.""" + options = [ + ("TRUE", "true"), + ("FALSE", "false"), + ] + if self.context.get("null"): + options.insert(0, ("", "")) + + return options + + async def get_queryset(self, request: Request, value: str, qs: QuerySet[Model]) -> QuerySet[Model]: + """Return filtered queryset.""" + filters = {self.context.get("name"): (value == "true")} + return qs.filter(**filters)