Filters
Full attribute and method reference for the filter system, generated from docstrings. For a task-oriented walkthrough, see Filters and Custom Filters.
The classes below are backend-agnostic: they declare a filter's name, label, and
data_type, but not its query logic. Each ORM backend (contrib.sqla, contrib.beanie,
contrib.mongoengine, contrib.tortoise) subclasses them to add the actual apply() implementation for that
backend. See the relevant integration page for the concrete,
importable filter classes.
Core types
starlette_admin.filters.base.FilterDataType
Bases: StrEnum
The kind of value a BaseFilter operates on. This informs the filter builder UI which input widget to render for the filter's value(s), and indicates when no input is needed for value-less filters.
Attributes:
| Name | Type | Description |
|---|---|---|
STRING |
Plain text input. |
|
NUMBER |
Numeric input. |
|
DATE |
Date picker ( |
|
DATETIME |
Date and time picker ( |
|
TIME |
Time picker ( |
|
BOOLEAN |
True/false toggle. |
|
ENUM |
Single or multi-select dropdown. |
|
ARRAY |
Free-text tag input (Select2 tags) (for list or array fields such as |
|
NONE |
No input (for filters that do not require a value, e.g., "is null"). |
Source code in starlette_admin/filters/base.py
starlette_admin.filters.base.BaseFilter
Bases: ABC
Base class for all filters.
A filter is responsible for three things:
- Declaring what kind of value it needs (
data_type,has_value2) so the filter builder UI can render the appropriate input(s). - Parsing the raw (string) value from the URL into the value
apply()will receive (parse_value), raising FilterValidationError if it is not acceptable. - Applying itself to a query (
apply). Given a FilterApplyContext with all necessary context, it returns the modified query. It never reads global state, which ensures its portability across backends. Each ORM contrib package provides concrete subclasses that implementapplyfor its own query representation (seeget_filter_registry()on the relevantModelView).
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
A slug identifying this filter, e.g., |
label |
str
|
A human-readable label shown in the filter builder dropdown. |
data_type |
FilterDataType
|
The kind of value this filter operates on. This informs the filter builder UI which input widget to render for the value(s). [FilterDataType.NONE][starlette_admin.types.FilterDataType] means the filter requires no value at all (e.g., "is null" or "is true"), so the UI renders no input, and |
has_value2 |
bool
|
|
Source code in starlette_admin/filters/base.py
apply(ctx)
abstractmethod
Apply this filter to a query.
This method receives a FilterApplyContext with all necessary information and must return the modified query object. Implementations must not mutate ctx.query in place for dict-based backends (e.g., PyMongo); instead, they should return a new or updated copy.
Source code in starlette_admin/filters/base.py
get_choices(request)
Optional (value, label) pairs for this filter's value picker, independent of the field's own choices (EnumField is the only field that supplies choices on its own).
Override this for a filter whose value should come from a dynamic, per-request list rather than a typed-in string, e.g. an "is one of" filter over a relation field where the value is a foreign key but the picker should show a human-readable label. A non-empty result here makes the filter builder render this filter's value input as a dropdown seeded with these pairs, taking precedence over the field's own choices.
The default implementation returns None, leaving the value input as the plain input the filter's data_type implies (or the field's own choices, for a field like EnumField that supplies them).
Source code in starlette_admin/filters/base.py
parse_value(raw)
Parse the raw string value from the URL into the value apply() will receive as ctx.value (or ctx.value2).
Override this method to convert values into richer types, e.g., Decimal, date, etc. The default implementation passes the raw string through unchanged.
Source code in starlette_admin/filters/base.py
starlette_admin.filters.base.FilterApplyContext
dataclass
All the arguments an [apply][starlette_admin.filters.BaseFilter.apply] implementation will ever need, bundled to ensure that adding new context (e.g., locale, timezone) later is a backwards-compatible change. Existing subclasses that ignore the new attributes will continue to function unchanged.
Attributes:
| Name | Type | Description |
|---|---|---|
query |
Any
|
The ORM-specific query or filter object being constructed (e.g., a SQLAlchemy |
field_name |
str
|
The name of the field or column to filter on. |
value |
Any
|
The parsed (not raw) primary value, as returned by [parse_value][starlette_admin.filters.BaseFilter.parse_value]. |
value2 |
Any
|
The parsed secondary value. This is only meaningful when |
request |
Request | None
|
The current request, available for user or locale-aware filters. |
view |
Optional[BaseModelView]
|
The view performing the query. |
Source code in starlette_admin/filters/base.py
starlette_admin.filters.base.FilterValidationError
Bases: StarletteAdminException
Raised by [BaseFilter.parse_value][starlette_admin.filters.BaseFilter.parse_value]
when a raw value isn't acceptable for that filter (wrong format, out of
range, ...). _parse_list_params catches this and rejects the request with
HTTP 400 before any ORM code runs.
Source code in starlette_admin/filters/base.py
starlette_admin.filters.base.FilterRule
dataclass
A single {field, filter, value} leaf in a filter tree, as deserialized from the filter query parameter JSON (see FilterGroup).
Attributes:
| Name | Type | Description |
|---|---|---|
field |
str
|
The name of the field to filter on. |
filter |
str
|
The slug of the BaseFilter to apply (e.g., |
value |
Any
|
The raw value as received from the client; this is replaced with the parsed value once |
value2 |
Any
|
The raw secondary value; only present for filters with |
Source code in starlette_admin/filters/base.py
starlette_admin.filters.base.FilterGroup
dataclass
A node in the filter tree: either a leaf FilterRule or a nested AND/OR group of rules or sub-groups, mirroring the JSON the filter builder serialises into the filter query parameter:
{
"logic": "and",
"rules": [
{"field": "name", "filter": "contains", "value": "john"},
{
"logic": "or",
"rules": [
{"field": "age", "filter": "gte", "value": "18"},
{"field": "verified", "filter": "is_true"}
]
}
]
}
Attributes:
| Name | Type | Description |
|---|---|---|
logic |
str
|
How |
rules |
list[Union[FilterGroup, FilterRule]]
|
The child rules or groups. Empty groups apply no filtering. |
Source code in starlette_admin/filters/base.py
starlette_admin.filters.registry.FilterRegistry
Maps BaseField subclasses to the BaseFilter classes available for
them.
A subclass declares one @filters(FieldType, ...)-decorated method per
field type it supports. filters_for resolves a given field to its
filters by walking type(field).__mro__ and returning the first
registered match, so a field type that has no entry of its own falls back
to whatever its nearest registered ancestor declares.
Each ORM contrib package owns one instance of this registry, populated
with its own concrete BaseFilter subclasses, and hands it back from
ModelView.get_filter_registry(). To change the filters offered for a
field type, subclass the contrib registry and override just that one
method rather than rebuilding the registry from scratch.
Source code in starlette_admin/filters/registry.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
filters_for(field)
Return the filter classes available for field.
A field's own filters attribute, if set, takes precedence over
anything registered here. Otherwise this walks type(field).__mro__
from most to least specific and returns the filters registered for
the first matching type. For example, EmailField (a StringField
subclass) inherits StringField's filters unless EmailField has its
own registration. Returns an empty list if no type in the MRO is
registered.
Source code in starlette_admin/filters/registry.py
get_filter(field, name)
Return the filter class among filters_for(field) whose
BaseFilter.name slug matches name, or None if none match.
Two callers rely on this: request parsing uses it to validate a
filter slug coming from the URL, and each backend uses it to resolve
an already-parsed FilterRule
back into the concrete filter class whose apply() builds the query
fragment.
Source code in starlette_admin/filters/registry.py
register(field_type, *filter_classes)
Set filter_classes as the filters available for field_type.
This is the imperative counterpart to the declarative @filters
methods, useful for registering filters at runtime rather than on a
subclass. Calling it again for the same field_type replaces the
previous registration outright; it does not merge with it.
Source code in starlette_admin/filters/registry.py
starlette_admin.filters.registry.filters(*field_types)
Class method decorator that registers the decorated method as the
source of filters for one or more BaseField subclasses.
Apply it to a method on a FilterRegistry subclass. FilterRegistry.__init__
scans the instance for methods carrying this marker and indexes each by
the field_types passed here, so filters_for can look the method up
later by field type.
Source code in starlette_admin/filters/registry.py
Generic
starlette_admin.filters.generic.EqualFilter
Bases: BaseFilter
field == value. Generic equality, suitable for strings, enums and any
other field whose values can be compared as-is. Numbers and temporal
fields get their own equality filters (see numeric/date) since they
need type-aware parsing.
Source code in starlette_admin/filters/generic.py
starlette_admin.filters.generic.NotEqualFilter
Bases: BaseFilter
field != value. The negated counterpart of
EqualFilter.
Source code in starlette_admin/filters/generic.py
starlette_admin.filters.generic.IsNullFilter
Bases: BaseFilter
field IS NULL (or empty, for collection-like fields). Needs no value.
Source code in starlette_admin/filters/generic.py
starlette_admin.filters.generic.IsNotNullFilter
Bases: BaseFilter
field IS NOT NULL (or non-empty, for collection-like fields).
Needs no value.
Source code in starlette_admin/filters/generic.py
Numeric
starlette_admin.filters.numeric.EqualFilter
Bases: BaseFilter
field == value for numeric fields.
Source code in starlette_admin/filters/numeric.py
starlette_admin.filters.numeric.NotEqualFilter
Bases: BaseFilter
field != value for numeric fields.
Source code in starlette_admin/filters/numeric.py
starlette_admin.filters.numeric.GreaterThanFilter
starlette_admin.filters.numeric.LessThanFilter
starlette_admin.filters.numeric.GreaterThanOrEqualFilter
starlette_admin.filters.numeric.LessThanOrEqualFilter
starlette_admin.filters.numeric.BetweenFilter
String
starlette_admin.filters.string.ContainsFilter
Bases: BaseFilter
field contains value (case-insensitive substring match).
Source code in starlette_admin/filters/string.py
starlette_admin.filters.string.NotContainsFilter
Bases: BaseFilter
The negated counterpart of ContainsFilter.
Source code in starlette_admin/filters/string.py
starlette_admin.filters.string.StartsWithFilter
starlette_admin.filters.string.EndsWithFilter
Boolean
starlette_admin.filters.boolean.IsTrueFilter
Bases: BaseFilter
field IS TRUE. Needs no value: selecting it from the filter builder
is the assertion.
Source code in starlette_admin/filters/boolean.py
starlette_admin.filters.boolean.IsFalseFilter
Bases: BaseFilter
field IS FALSE. Needs no value, see
IsTrueFilter.
Source code in starlette_admin/filters/boolean.py
Date and time
starlette_admin.filters.date.DateEqualFilter
Bases: BaseFilter
field == value, for DateField.
DateTimeEqualFilter and TimeEqualFilter cover DateTimeField/ArrowField and TimeField respectively; they exhibit the same behavior, but with a different data_type (and thus a different input widget or parsed type).
Source code in starlette_admin/filters/date.py
starlette_admin.filters.date.DateTimeEqualFilter
starlette_admin.filters.date.TimeEqualFilter
starlette_admin.filters.date.DateBetweenFilter
Bases: BaseFilter
value <= field <= value2, for
DateField. See
DateEqualFilter for how
DateTimeBetweenFilter
and TimeBetweenFilter
relate to it.
Source code in starlette_admin/filters/date.py
starlette_admin.filters.date.DateTimeBetweenFilter
starlette_admin.filters.date.TimeBetweenFilter
starlette_admin.filters.date.DateInPastFilter
Bases: BaseFilter
field is before now. This filter needs no value; the comparison point is always "now", evaluated by the backend at query time (e.g., SQL NOW()), so this filter applies unchanged to date, datetime, and time fields alike.
Source code in starlette_admin/filters/date.py
starlette_admin.filters.date.DateInFutureFilter
Bases: BaseFilter
field is after now. See
DateInPastFilter.
Source code in starlette_admin/filters/date.py
Enum
starlette_admin.filters.enum.InFilter
Bases: BaseFilter
field IN (values). Renders as a multi-select in the filter builder.
Source code in starlette_admin/filters/enum.py
starlette_admin.filters.enum.NotInFilter
Bases: BaseFilter
field NOT IN (values). The negated counterpart of
InFilter.
Source code in starlette_admin/filters/enum.py
Array
starlette_admin.filters.array.InFilter
Bases: BaseFilter
List/array field contains at least one of the given values (tag input).
Source code in starlette_admin/filters/array.py
starlette_admin.filters.array.NotInFilter
Bases: BaseFilter
A list or array field contains none of the given values (tag input).