Skip to content

filter_action

FilterAction

Bases: QueryAction

Represents a single WHERE predicate on a queryset.

Created for each key=value (or key__op=value) passed to filter(...) / exclude(...) and to filtering shortcuts like get(...) / first(...).

Source code in ormar/queryset/actions/filter_action.py
Python
 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class FilterAction(QueryAction):
    """
    Represents a single WHERE predicate on a queryset.

    Created for each ``key=value`` (or ``key__op=value``) passed to
    ``filter(...)`` / ``exclude(...)`` and to filtering shortcuts like
    ``get(...)`` / ``first(...)``.
    """

    def __init__(self, filter_str: str, value: Any, model_cls: type["Model"]) -> None:
        super().__init__(query_str=filter_str, model_cls=model_cls)
        self.expression_columns: dict[str, SelectAction] = {}
        if isinstance(value, Expression):
            value = value._node()
            if self.operator not in ("exact", "ne", "gt", "gte", "lt", "lte"):
                raise QueryDefinitionError(
                    f"Expressions only take exact, ne, gt, gte, lt and lte filters, "
                    f"not '{self.operator}'"
                )
            self.expression_columns = expression_columns(node=value, model=model_cls)
        self.filter_value = value
        self._escape_characters_in_clause()

    def path_actions(self) -> list[QueryAction]:
        """
        :return: the action and the fields its expression reads, each resolving
            one relation path to a joined table
        :rtype: list[QueryAction]
        """
        return [self, *self.expression_columns.values()]

    def update_select_related(self, select_related: list[str]) -> list[str]:
        """
        Add the joins of the filtered field and of the fields its expression
        reads (see ``QueryAction.update_select_related``).

        :param select_related: list of relation join strings
        :type select_related: list[str]
        :return: list of relation joins with implied joins from filter added
        :rtype: list[str]
        """
        select_related = super().update_select_related(select_related=select_related)
        for column in self.expression_columns.values():
            select_related = column.update_select_related(select_related=select_related)
        return select_related

    def has_escaped_characters(self) -> bool:
        """Check if value is a string that contains characters to escape"""
        return isinstance(self.filter_value, str) and any(
            c for c in ESCAPE_CHARACTERS if c in self.filter_value
        )

    def _split_value_into_parts(self, query_str: str) -> None:
        self.operator, path = split_filter_key(key=query_str)
        self.field_name = path[-1]
        self.related_parts = path[:-1]

    def _escape_characters_in_clause(self) -> None:
        """
        Escapes the special characters ["%", "_"] if needed.
        Adds `%` for `like` queries.

        :raises QueryDefinitionError: if contains or icontains is used with
        ormar model instance
        :return: escaped value and flag if escaping is needed
        :rtype: tuple[Any, bool]
        """
        self.has_escaped_character = False
        if self.operator in [
            "contains",
            "icontains",
            "startswith",
            "istartswith",
            "endswith",
            "iendswith",
        ]:
            if isinstance(self.filter_value, ormar.Model):
                raise QueryDefinitionError(
                    "You cannot use contains and icontains with instance of the Model"
                )
            self.has_escaped_character = self.has_escaped_characters()
            if self.has_escaped_character:
                self._escape_chars()
            self._prefix_suffix_quote()

    def _escape_chars(self) -> None:
        """Actually replaces chars to escape in value"""
        for char in ESCAPE_CHARACTERS:
            self.filter_value = self.filter_value.replace(char, f"\\{char}")

    def _prefix_suffix_quote(self) -> None:
        """
        Adds % to the beginning of the value if operator checks for containment and not
        starts with.

        Adds % to the end of the value if operator checks for containment and not
        end with.
        :return:
        :rtype:
        """
        prefix = "%" if "start" not in self.operator else ""
        sufix = "%" if "end" not in self.operator else ""
        self.filter_value = f"{prefix}{self.filter_value}{sufix}"

    def bound_value(self) -> Any:
        """
        Return the value bound to the query: an ormar Model is replaced by its
        value it is referenced by (its primary key or the foreign key's
        ``to_field``), so ``filter(author=author)`` binds ``author.pk``.

        ``filter_value`` itself is left untouched, so the action can be read
        again on every call to collect the value for a cached template.

        :return: value to bind
        :rtype: Any
        """
        if isinstance(self.filter_value, ormar.Model):
            field = self.target_model.ormar_config.model_fields.get(self.field_name)
            if (
                field is not None
                and field.is_relation
                and not field.virtual
                and not field.is_multi
            ):
                relation_field = cast("ForeignKeyField", field)
                return relation_field.target_value_of(related=self.filter_value)
            return self.filter_value.pk
        return self.filter_value

    def binds_slot(self) -> bool:
        """
        Return whether the value is bound as a parameter: SQLAlchemy renders
        ``None`` (``IS NULL``), booleans and ``isnull`` checks into the SQL text.

        Only values that are bound can get a named slot in a statement
        template. The others stay in the SQL text and become part of the
        template's cache key instead (see ``templates.value_cache_key``)::

            filter(name="a")  # True  -> name = :ormar_p0
            filter(name=None)  # False -> name IS NULL

        :return: True when the value is sent as a bound parameter
        :rtype: bool
        """
        value = self.bound_value()
        return (
            self.operator != "isnull"
            and value is not None
            and not isinstance(value, bool)
        )

    def get_text_clause(self, binder: Optional["SlotBinder"] = None) -> TextClause:
        """
        Escapes characters if it's required.
        Substitutes values of the models if value is a ormar Model with its pk value.
        Compiles the clause.

        :param binder: names the bound value for statement templates
            (``= :ormar_p0`` instead of an anonymous parameter), if given
        :type binder: Optional[SlotBinder]
        :return: complied and escaped clause
        :rtype: sqlalchemy.sql.elements.TextClause
        """
        value = self.bound_value()
        op_attr = FILTER_OPERATORS[self.operator]
        if self.operator == "isnull":
            op_attr = "is_" if value else "isnot"
            filter_value = None
        elif isinstance(value, Expression):
            filter_value = expression_clause(
                node=value,
                columns=self.expression_columns,
                slot=binder.slots[id(self)] if binder is not None else None,
            )
        else:
            filter_value = value
            if binder is not None and self.binds_slot():
                filter_value = binder.bind(action=self, value=value)
        clause = getattr(self.aliased_column, op_attr)(filter_value)
        if self.has_escaped_character:
            clause.modifiers["escape"] = "\\"
        return clause

binds_slot()

Return whether the value is bound as a parameter: SQLAlchemy renders None (IS NULL), booleans and isnull checks into the SQL text.

Only values that are bound can get a named slot in a statement template. The others stay in the SQL text and become part of the template's cache key instead (see templates.value_cache_key)::

Text Only
1
2
filter(name="a")  # True  -> name = :ormar_p0
filter(name=None)  # False -> name IS NULL

Returns:

Type Description
bool

True when the value is sent as a bound parameter

Source code in ormar/queryset/actions/filter_action.py
Python
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def binds_slot(self) -> bool:
    """
    Return whether the value is bound as a parameter: SQLAlchemy renders
    ``None`` (``IS NULL``), booleans and ``isnull`` checks into the SQL text.

    Only values that are bound can get a named slot in a statement
    template. The others stay in the SQL text and become part of the
    template's cache key instead (see ``templates.value_cache_key``)::

        filter(name="a")  # True  -> name = :ormar_p0
        filter(name=None)  # False -> name IS NULL

    :return: True when the value is sent as a bound parameter
    :rtype: bool
    """
    value = self.bound_value()
    return (
        self.operator != "isnull"
        and value is not None
        and not isinstance(value, bool)
    )

bound_value()

Return the value bound to the query: an ormar Model is replaced by its value it is referenced by (its primary key or the foreign key's to_field), so filter(author=author) binds author.pk.

filter_value itself is left untouched, so the action can be read again on every call to collect the value for a cached template.

Returns:

Type Description
Any

value to bind

Source code in ormar/queryset/actions/filter_action.py
Python
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def bound_value(self) -> Any:
    """
    Return the value bound to the query: an ormar Model is replaced by its
    value it is referenced by (its primary key or the foreign key's
    ``to_field``), so ``filter(author=author)`` binds ``author.pk``.

    ``filter_value`` itself is left untouched, so the action can be read
    again on every call to collect the value for a cached template.

    :return: value to bind
    :rtype: Any
    """
    if isinstance(self.filter_value, ormar.Model):
        field = self.target_model.ormar_config.model_fields.get(self.field_name)
        if (
            field is not None
            and field.is_relation
            and not field.virtual
            and not field.is_multi
        ):
            relation_field = cast("ForeignKeyField", field)
            return relation_field.target_value_of(related=self.filter_value)
        return self.filter_value.pk
    return self.filter_value

get_text_clause(binder=None)

Escapes characters if it's required. Substitutes values of the models if value is a ormar Model with its pk value. Compiles the clause.

Parameters:

Name Type Description Default
binder Optional[SlotBinder]

names the bound value for statement templates (= :ormar_p0 instead of an anonymous parameter), if given

None

Returns:

Type Description
sqlalchemy.sql.elements.TextClause

complied and escaped clause

Source code in ormar/queryset/actions/filter_action.py
Python
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def get_text_clause(self, binder: Optional["SlotBinder"] = None) -> TextClause:
    """
    Escapes characters if it's required.
    Substitutes values of the models if value is a ormar Model with its pk value.
    Compiles the clause.

    :param binder: names the bound value for statement templates
        (``= :ormar_p0`` instead of an anonymous parameter), if given
    :type binder: Optional[SlotBinder]
    :return: complied and escaped clause
    :rtype: sqlalchemy.sql.elements.TextClause
    """
    value = self.bound_value()
    op_attr = FILTER_OPERATORS[self.operator]
    if self.operator == "isnull":
        op_attr = "is_" if value else "isnot"
        filter_value = None
    elif isinstance(value, Expression):
        filter_value = expression_clause(
            node=value,
            columns=self.expression_columns,
            slot=binder.slots[id(self)] if binder is not None else None,
        )
    else:
        filter_value = value
        if binder is not None and self.binds_slot():
            filter_value = binder.bind(action=self, value=value)
    clause = getattr(self.aliased_column, op_attr)(filter_value)
    if self.has_escaped_character:
        clause.modifiers["escape"] = "\\"
    return clause

has_escaped_characters()

Check if value is a string that contains characters to escape

Source code in ormar/queryset/actions/filter_action.py
Python
126
127
128
129
130
def has_escaped_characters(self) -> bool:
    """Check if value is a string that contains characters to escape"""
    return isinstance(self.filter_value, str) and any(
        c for c in ESCAPE_CHARACTERS if c in self.filter_value
    )

path_actions()

Returns:

Type Description
list[QueryAction]

the action and the fields its expression reads, each resolving one relation path to a joined table

Source code in ormar/queryset/actions/filter_action.py
Python
103
104
105
106
107
108
109
def path_actions(self) -> list[QueryAction]:
    """
    :return: the action and the fields its expression reads, each resolving
        one relation path to a joined table
    :rtype: list[QueryAction]
    """
    return [self, *self.expression_columns.values()]

Add the joins of the filtered field and of the fields its expression reads (see QueryAction.update_select_related).

Parameters:

Name Type Description Default
select_related list[str]

list of relation join strings

required

Returns:

Type Description
list[str]

list of relation joins with implied joins from filter added

Source code in ormar/queryset/actions/filter_action.py
Python
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def update_select_related(self, select_related: list[str]) -> list[str]:
    """
    Add the joins of the filtered field and of the fields its expression
    reads (see ``QueryAction.update_select_related``).

    :param select_related: list of relation join strings
    :type select_related: list[str]
    :return: list of relation joins with implied joins from filter added
    :rtype: list[str]
    """
    select_related = super().update_select_related(select_related=select_related)
    for column in self.expression_columns.values():
        select_related = column.update_select_related(select_related=select_related)
    return select_related

KeyInFilterAction

Bases: FilterAction

pk__in filter on a composite primary key, compiled to one row-value IN over the key columns::

Text Only
1
2
filter(pk__in=[(1, 7), (2, 8)])
# -> WHERE (orders.tenant_id, orders.number) IN ((1, 7), (2, 8))

The values are bound as one expanding parameter, so every list length shares one statement. With columns the action compares other columns of the table, e.g. the columns of a foreign key stored in several columns (anchor names the field the columns belong to)::

Text Only
1
2
filter(order__in=[(1, 7)])
# -> WHERE (lines.ord_tenant, lines.ord_no) IN ((1, 7))
Source code in ormar/queryset/actions/filter_action.py
Python
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
class KeyInFilterAction(FilterAction):
    """
    ``pk__in`` filter on a composite primary key, compiled to one row-value
    ``IN`` over the key columns::

        filter(pk__in=[(1, 7), (2, 8)])
        # -> WHERE (orders.tenant_id, orders.number) IN ((1, 7), (2, 8))

    The values are bound as one expanding parameter, so every list length
    shares one statement. With ``columns`` the action compares other columns
    of the table, e.g. the columns of a foreign key stored in several columns
    (``anchor`` names the field the columns belong to)::

        filter(order__in=[(1, 7)])
        # -> WHERE (lines.ord_tenant, lines.ord_no) IN ((1, 7))
    """

    def __init__(
        self,
        *,
        prefix: str,
        values: list[tuple[Any, ...]],
        model_cls: type["Model"],
        anchor: Optional[str] = None,
        columns: Optional[tuple[str, ...]] = None,
    ) -> None:
        """
        :param prefix: relation path to the filtered model, ``""`` or ending in
            ``__``
        :type prefix: str
        :param values: key values as tuples of parts in key order
        :type values: list[tuple[Any, ...]]
        :param model_cls: queried model
        :type model_cls: type[Model]
        """
        self.columns = columns
        if anchor is None:
            target_key = model_cls.ormar_config.pk
            for part in prefix.split("__")[:-1]:
                target_key = target_key.owner.ormar_config.model_fields[
                    part
                ].to.ormar_config.pk
            anchor = target_key.fields[0]
        super().__init__(
            filter_str=f"{prefix}{anchor}__{self.key_operator()}",
            value=values,
            model_cls=model_cls,
        )
        key_name = "pk" if columns is None else "__".join(columns)
        self.query_str = f"{prefix}{key_name}__{self.key_operator()}"

    @staticmethod
    def key_operator() -> str:
        """
        :return: lookup operator of the action
        :rtype: str
        """
        return "in"

    def key_columns(self) -> list[sqlalchemy.Column]:
        """
        :return: columns of the key, aliased to the table prefix of the filter
        :rtype: list[sqlalchemy.Column]
        """
        names = self.columns or tuple(
            column.name for column in self.target_model.ormar_config.pk.columns()
        )
        return [self.aliased_table.c[name] for name in names]

    def get_text_clause(self, binder: Optional["SlotBinder"] = None) -> TextClause:
        """
        :param binder: names the bound values for statement templates, if given
        :type binder: Optional[SlotBinder]
        :return: row-value ``IN`` over the key columns
        :rtype: TextClause
        """
        columns = self.key_columns()
        value: Any = self.filter_value
        if binder is not None:
            value = binder.bind_key_in(
                action=self,
                value=value,
                column_types=[column.type for column in columns],
            )
        return cast(TextClause, sqlalchemy.tuple_(*columns).in_(value))

__init__(*, prefix, values, model_cls, anchor=None, columns=None)

Parameters:

Name Type Description Default
prefix str

relation path to the filtered model, "" or ending in __

required
values list[tuple[Any, ...]]

key values as tuples of parts in key order

required
model_cls type[Model]

queried model

required
Source code in ormar/queryset/actions/filter_action.py
Python
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def __init__(
    self,
    *,
    prefix: str,
    values: list[tuple[Any, ...]],
    model_cls: type["Model"],
    anchor: Optional[str] = None,
    columns: Optional[tuple[str, ...]] = None,
) -> None:
    """
    :param prefix: relation path to the filtered model, ``""`` or ending in
        ``__``
    :type prefix: str
    :param values: key values as tuples of parts in key order
    :type values: list[tuple[Any, ...]]
    :param model_cls: queried model
    :type model_cls: type[Model]
    """
    self.columns = columns
    if anchor is None:
        target_key = model_cls.ormar_config.pk
        for part in prefix.split("__")[:-1]:
            target_key = target_key.owner.ormar_config.model_fields[
                part
            ].to.ormar_config.pk
        anchor = target_key.fields[0]
    super().__init__(
        filter_str=f"{prefix}{anchor}__{self.key_operator()}",
        value=values,
        model_cls=model_cls,
    )
    key_name = "pk" if columns is None else "__".join(columns)
    self.query_str = f"{prefix}{key_name}__{self.key_operator()}"

get_text_clause(binder=None)

Parameters:

Name Type Description Default
binder Optional[SlotBinder]

names the bound values for statement templates, if given

None

Returns:

Type Description
TextClause

row-value IN over the key columns

Source code in ormar/queryset/actions/filter_action.py
Python
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
def get_text_clause(self, binder: Optional["SlotBinder"] = None) -> TextClause:
    """
    :param binder: names the bound values for statement templates, if given
    :type binder: Optional[SlotBinder]
    :return: row-value ``IN`` over the key columns
    :rtype: TextClause
    """
    columns = self.key_columns()
    value: Any = self.filter_value
    if binder is not None:
        value = binder.bind_key_in(
            action=self,
            value=value,
            column_types=[column.type for column in columns],
        )
    return cast(TextClause, sqlalchemy.tuple_(*columns).in_(value))

key_columns()

Returns:

Type Description
list[sqlalchemy.Column]

columns of the key, aliased to the table prefix of the filter

Source code in ormar/queryset/actions/filter_action.py
Python
323
324
325
326
327
328
329
330
331
def key_columns(self) -> list[sqlalchemy.Column]:
    """
    :return: columns of the key, aliased to the table prefix of the filter
    :rtype: list[sqlalchemy.Column]
    """
    names = self.columns or tuple(
        column.name for column in self.target_model.ormar_config.pk.columns()
    )
    return [self.aliased_table.c[name] for name in names]

key_operator() staticmethod

Returns:

Type Description
str

lookup operator of the action

Source code in ormar/queryset/actions/filter_action.py
Python
315
316
317
318
319
320
321
@staticmethod
def key_operator() -> str:
    """
    :return: lookup operator of the action
    :rtype: str
    """
    return "in"

KeyNullFilterAction

Bases: KeyInFilterAction

isnull filter on a foreign key stored in several columns: a relation with any column null references no row, so it counts as missing::

Text Only
1
2
filter(order__isnull=True)
# -> WHERE lines.ord_tenant IS NULL OR lines.ord_no IS NULL
Source code in ormar/queryset/actions/filter_action.py
Python
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
class KeyNullFilterAction(KeyInFilterAction):
    """
    ``isnull`` filter on a foreign key stored in several columns: a relation
    with any column null references no row, so it counts as missing::

        filter(order__isnull=True)
        # -> WHERE lines.ord_tenant IS NULL OR lines.ord_no IS NULL
    """

    @staticmethod
    def key_operator() -> str:
        """
        :return: lookup operator of the action
        :rtype: str
        """
        return "isnull"

    def get_text_clause(self, binder: Optional["SlotBinder"] = None) -> TextClause:
        """
        :param binder: slot binder of a statement template (unused, nothing is
            bound)
        :type binder: Optional[SlotBinder]
        :return: the null checks of the key columns
        :rtype: TextClause
        """
        columns = self.key_columns()
        if self.filter_value:
            return cast(
                TextClause, sqlalchemy.or_(*(column.is_(None) for column in columns))
            )
        return cast(
            TextClause, sqlalchemy.and_(*(column.isnot(None) for column in columns))
        )

get_text_clause(binder=None)

Parameters:

Name Type Description Default
binder Optional[SlotBinder]

slot binder of a statement template (unused, nothing is bound)

None

Returns:

Type Description
TextClause

the null checks of the key columns

Source code in ormar/queryset/actions/filter_action.py
Python
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
def get_text_clause(self, binder: Optional["SlotBinder"] = None) -> TextClause:
    """
    :param binder: slot binder of a statement template (unused, nothing is
        bound)
    :type binder: Optional[SlotBinder]
    :return: the null checks of the key columns
    :rtype: TextClause
    """
    columns = self.key_columns()
    if self.filter_value:
        return cast(
            TextClause, sqlalchemy.or_(*(column.is_(None) for column in columns))
        )
    return cast(
        TextClause, sqlalchemy.and_(*(column.isnot(None) for column in columns))
    )

key_operator() staticmethod

Returns:

Type Description
str

lookup operator of the action

Source code in ormar/queryset/actions/filter_action.py
Python
360
361
362
363
364
365
366
@staticmethod
def key_operator() -> str:
    """
    :return: lookup operator of the action
    :rtype: str
    """
    return "isnull"

split_filter_key(key)

Split a filter key into its operator and the path of the filtered field. A trailing operator name is always the operator, so a field named like an operator is filtered with an explicit one::

Text Only
1
2
split_filter_key(key="album__name__ne")  # -> ("ne", ["album", "name"])
split_filter_key(key="gt__exact")  # field gt -> ("exact", ["gt"])

Parameters:

Name Type Description Default
key str

filter key

required

Returns:

Type Description
tuple[str, list[str]]

operator (exact when none is given) and the field path parts

Raises:

Type Description
QueryDefinitionError

if the key is only an operator

Source code in ormar/queryset/actions/filter_action.py
Python
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def split_filter_key(key: str) -> tuple[str, list[str]]:
    """
    Split a filter key into its operator and the path of the filtered field.
    A trailing operator name is always the operator, so a field named like an
    operator is filtered with an explicit one::

        split_filter_key(key="album__name__ne")  # -> ("ne", ["album", "name"])
        split_filter_key(key="gt__exact")  # field gt -> ("exact", ["gt"])

    :param key: filter key
    :type key: str
    :raises QueryDefinitionError: if the key is only an operator
    :return: operator (``exact`` when none is given) and the field path parts
    :rtype: tuple[str, list[str]]
    """
    parts = key.split("__")
    if parts[-1] not in FILTER_OPERATORS:
        return "exact", parts
    if len(parts) == 1:
        raise QueryDefinitionError(
            f"Filter key '{key}' is an operator without a field, "
            f"filter a field named '{key}' with '{key}__exact'"
        )
    return parts[-1], parts[:-1]