mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2025-08-14 02:28:40 +08:00
Add filter class for boolean fields
This commit is contained in:
@ -2,7 +2,9 @@
|
||||
|
||||
## 1.0
|
||||
|
||||
### 0.1.4
|
||||
### 1.0.4
|
||||
|
||||
- Add `widgets.filters.Boolean` class
|
||||
|
||||
### 1.0.3
|
||||
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user