Skip to content

field_accessor

FieldAccessor

Bases: Expression

Helper to access ormar fields directly from Model class also for nested models attributes.

An accessor of a non-relation field is a column expression as well: Slot.taken < Slot.capacity, update(taken=Slot.taken + 1).

Source code in ormar/queryset/field_accessor.py
Python
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 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
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
262
263
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
class FieldAccessor(Expression):
    """
    Helper to access ormar fields directly from Model class also for nested
    models attributes.

    An accessor of a non-relation field is a column expression as well:
    ``Slot.taken < Slot.capacity``, ``update(taken=Slot.taken + 1)``.
    """

    def __init__(
        self,
        source_model: type["Model"],
        field: Optional["BaseField"] = None,
        model: Optional[type["Model"]] = None,
        access_chain: str = "",
        is_key: bool = False,
    ) -> None:
        self._source_model = source_model
        self._field = field
        self._model = model
        self._access_chain = access_chain
        self._is_key = is_key

    def __getattr__(self, item: str) -> Any:
        """
        Accessor return new accessor for each field and nested models.
        Thanks to that operator overload is possible to use in filter.

        :param item: attribute name
        :type item: str
        :return: FieldAccessor for field or nested model
        :rtype: ormar.queryset.field_accessor.FieldAccessor
        """
        if (
            object.__getattribute__(self, "_field")
            and item == object.__getattribute__(self, "_field").name
        ):
            return self._field

        if item == "pk" and object.__getattribute__(self, "_model"):
            return key_accessor(
                source_model=self._source_model,
                model=cast(type["Model"], self._model),
                prefix=self._access_chain + "__",
            )

        if (
            object.__getattribute__(self, "_model")
            and item
            in object.__getattribute__(self, "_model").ormar_config.model_fields
        ):
            field = cast("Model", self._model).ormar_config.model_fields[item]
            if field.is_relation:
                return FieldAccessor(
                    source_model=self._source_model,
                    model=field.to,
                    access_chain=self._access_chain + f"__{item}",
                )
            else:
                return FieldAccessor(
                    source_model=self._source_model,
                    field=field,
                    access_chain=self._access_chain + f"__{item}",
                )
        return object.__getattribute__(self, item)  # pragma: no cover

    def _check_field(self) -> None:
        if self._field or self._is_key:
            return
        field = self._source_model.ormar_config.model_fields.get(self._access_chain)
        if field is not None and not field.virtual and not field.is_multi:
            return
        raise AttributeError("Cannot filter by Model, you need to provide model name")

    def _node(self) -> F:
        """
        :raises QueryDefinitionError: for relation and composite key accessors
        :return: the field as an ``F`` node, which survives the deep copies of
            prefetch querysets
        :rtype: F
        """
        if self._field is None:
            raise QueryDefinitionError(
                "Only non-relation fields can be used in expressions"
            )
        return F(path=self._access_chain)

    def _select_operator(self, op: str, other: Any) -> FilterGroup:
        self._check_field()
        filter_kwg = {self._access_chain + f"__{METHODS_TO_OPERATORS[op]}": other}
        return FilterGroup(**filter_kwg)

    def __eq__(self, other: Any) -> FilterGroup:  # type: ignore
        """
        overloaded to work as sql `column = <VALUE>`

        :param other: value to check against operator
        :type other: Any
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__eq__", other=other)

    def __ne__(self, other: Any) -> FilterGroup:  # type: ignore
        """
        overloaded to work as sql `column != <VALUE>` (`IS NOT NULL` for None)

        :param other: value to check against operator
        :type other: Any
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__ne__", other=other)

    def __ge__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column >= <VALUE>`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__ge__", other=other)

    def __gt__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column > <VALUE>`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__gt__", other=other)

    def __le__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column <= <VALUE>`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__le__", other=other)

    def __lt__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column < <VALUE>`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__lt__", other=other)

    def __mod__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column LIKE '%<VALUE>%'`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__mod__", other=other)

    def __lshift__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column IN (<VALUE1>, <VALUE2>,...)`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="in", other=other)

    def __rshift__(self, other: Any) -> FilterGroup:
        """
        overloaded to work as sql `column IS NULL`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="isnull", other=True)

    def in_(self, other: Any) -> FilterGroup:
        """
        works as sql `column IN (<VALUE1>, <VALUE2>,...)`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="in", other=other)

    def iexact(self, other: Any) -> FilterGroup:
        """
        works as sql `column = <VALUE>` case-insensitive

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="iexact", other=other)

    def contains(self, other: Any) -> FilterGroup:
        """
        works as sql `column LIKE '%<VALUE>%'`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="contains", other=other)

    def icontains(self, other: Any) -> FilterGroup:
        """
        works as sql `column LIKE '%<VALUE>%'` case-insensitive

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="icontains", other=other)

    def startswith(self, other: Any) -> FilterGroup:
        """
        works as sql `column LIKE '<VALUE>%'`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="startswith", other=other)

    def istartswith(self, other: Any) -> FilterGroup:
        """
        works as sql `column LIKE '%<VALUE>'` case-insensitive

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="istartswith", other=other)

    def endswith(self, other: Any) -> FilterGroup:
        """
        works as sql `column LIKE '%<VALUE>'`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="endswith", other=other)

    def iendswith(self, other: Any) -> FilterGroup:
        """
        works as sql `column LIKE '%<VALUE>'` case-insensitive

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="iendswith", other=other)

    def isnull(self, other: Any) -> FilterGroup:
        """
        works as sql `column IS NULL` or `IS NOT NULL`

        :param other: value to check agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="isnull", other=other)

    def asc(self, nulls_ordering: Optional[NullsOrdering] = None) -> OrderAction:
        """
        works as sql `column asc`

        :param nulls_ordering: optional nulls placement, `NullsOrdering.FIRST`
            or `NullsOrdering.LAST`
        :type nulls_ordering: Optional[NullsOrdering]
        :raises ValueError: if `nulls_ordering` is not a `NullsOrdering` member
        :return: OrderGroup for operator
        :rtype: ormar.queryset.actions.OrderGroup
        """
        nulls_value = self._coerce_nulls_ordering(nulls_ordering)
        return OrderAction(
            order_str=self._access_chain,
            model_cls=self._source_model,
            nulls_ordering=nulls_value,
        )

    def desc(self, nulls_ordering: Optional[NullsOrdering] = None) -> OrderAction:
        """
        works as sql `column desc`

        :param nulls_ordering: optional nulls placement, `NullsOrdering.FIRST`
            or `NullsOrdering.LAST`
        :type nulls_ordering: Optional[NullsOrdering]
        :raises ValueError: if `nulls_ordering` is not a `NullsOrdering` member
        :return: OrderGroup for operator
        :rtype: ormar.queryset.actions.OrderGroup
        """
        nulls_value = self._coerce_nulls_ordering(nulls_ordering)
        return OrderAction(
            order_str="-" + self._access_chain,
            model_cls=self._source_model,
            nulls_ordering=nulls_value,
        )

    @staticmethod
    def _coerce_nulls_ordering(
        nulls_ordering: Optional[NullsOrdering],
    ) -> Optional[str]:
        if nulls_ordering is None:
            return None
        if not isinstance(nulls_ordering, NullsOrdering):
            raise ValueError("Invalid option for ordering nulls values.")
        return nulls_ordering.value

__eq__(other)

overloaded to work as sql column = <VALUE>

Parameters:

Name Type Description Default
other Any

value to check against operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
105
106
107
108
109
110
111
112
113
114
def __eq__(self, other: Any) -> FilterGroup:  # type: ignore
    """
    overloaded to work as sql `column = <VALUE>`

    :param other: value to check against operator
    :type other: Any
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__eq__", other=other)

__ge__(other)

overloaded to work as sql column >= <VALUE>

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
127
128
129
130
131
132
133
134
135
136
def __ge__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column >= <VALUE>`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__ge__", other=other)

__getattr__(item)

Accessor return new accessor for each field and nested models. Thanks to that operator overload is possible to use in filter.

Parameters:

Name Type Description Default
item str

attribute name

required

Returns:

Type Description
ormar.queryset.field_accessor.FieldAccessor

FieldAccessor for field or nested model

Source code in ormar/queryset/field_accessor.py
Python
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
def __getattr__(self, item: str) -> Any:
    """
    Accessor return new accessor for each field and nested models.
    Thanks to that operator overload is possible to use in filter.

    :param item: attribute name
    :type item: str
    :return: FieldAccessor for field or nested model
    :rtype: ormar.queryset.field_accessor.FieldAccessor
    """
    if (
        object.__getattribute__(self, "_field")
        and item == object.__getattribute__(self, "_field").name
    ):
        return self._field

    if item == "pk" and object.__getattribute__(self, "_model"):
        return key_accessor(
            source_model=self._source_model,
            model=cast(type["Model"], self._model),
            prefix=self._access_chain + "__",
        )

    if (
        object.__getattribute__(self, "_model")
        and item
        in object.__getattribute__(self, "_model").ormar_config.model_fields
    ):
        field = cast("Model", self._model).ormar_config.model_fields[item]
        if field.is_relation:
            return FieldAccessor(
                source_model=self._source_model,
                model=field.to,
                access_chain=self._access_chain + f"__{item}",
            )
        else:
            return FieldAccessor(
                source_model=self._source_model,
                field=field,
                access_chain=self._access_chain + f"__{item}",
            )
    return object.__getattribute__(self, item)  # pragma: no cover

__gt__(other)

overloaded to work as sql column > <VALUE>

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
138
139
140
141
142
143
144
145
146
147
def __gt__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column > <VALUE>`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__gt__", other=other)

__le__(other)

overloaded to work as sql column <= <VALUE>

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
149
150
151
152
153
154
155
156
157
158
def __le__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column <= <VALUE>`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__le__", other=other)

__lshift__(other)

overloaded to work as sql column IN (<VALUE1>, <VALUE2>,...)

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
182
183
184
185
186
187
188
189
190
191
def __lshift__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column IN (<VALUE1>, <VALUE2>,...)`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="in", other=other)

__lt__(other)

overloaded to work as sql column < <VALUE>

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
160
161
162
163
164
165
166
167
168
169
def __lt__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column < <VALUE>`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__lt__", other=other)

__mod__(other)

overloaded to work as sql column LIKE '%<VALUE>%'

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
171
172
173
174
175
176
177
178
179
180
def __mod__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column LIKE '%<VALUE>%'`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__mod__", other=other)

__ne__(other)

overloaded to work as sql column != <VALUE> (IS NOT NULL for None)

Parameters:

Name Type Description Default
other Any

value to check against operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
116
117
118
119
120
121
122
123
124
125
def __ne__(self, other: Any) -> FilterGroup:  # type: ignore
    """
    overloaded to work as sql `column != <VALUE>` (`IS NOT NULL` for None)

    :param other: value to check against operator
    :type other: Any
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="__ne__", other=other)

__rshift__(other)

overloaded to work as sql column IS NULL

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
193
194
195
196
197
198
199
200
201
202
def __rshift__(self, other: Any) -> FilterGroup:
    """
    overloaded to work as sql `column IS NULL`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="isnull", other=True)

asc(nulls_ordering=None)

works as sql column asc

Parameters:

Name Type Description Default
nulls_ordering Optional[NullsOrdering]

optional nulls placement, NullsOrdering.FIRST or NullsOrdering.LAST

None

Returns:

Type Description
ormar.queryset.actions.OrderGroup

OrderGroup for operator

Raises:

Type Description
ValueError

if nulls_ordering is not a NullsOrdering member

Source code in ormar/queryset/field_accessor.py
Python
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
def asc(self, nulls_ordering: Optional[NullsOrdering] = None) -> OrderAction:
    """
    works as sql `column asc`

    :param nulls_ordering: optional nulls placement, `NullsOrdering.FIRST`
        or `NullsOrdering.LAST`
    :type nulls_ordering: Optional[NullsOrdering]
    :raises ValueError: if `nulls_ordering` is not a `NullsOrdering` member
    :return: OrderGroup for operator
    :rtype: ormar.queryset.actions.OrderGroup
    """
    nulls_value = self._coerce_nulls_ordering(nulls_ordering)
    return OrderAction(
        order_str=self._access_chain,
        model_cls=self._source_model,
        nulls_ordering=nulls_value,
    )

contains(other)

works as sql column LIKE '%<VALUE>%'

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
226
227
228
229
230
231
232
233
234
235
def contains(self, other: Any) -> FilterGroup:
    """
    works as sql `column LIKE '%<VALUE>%'`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="contains", other=other)

desc(nulls_ordering=None)

works as sql column desc

Parameters:

Name Type Description Default
nulls_ordering Optional[NullsOrdering]

optional nulls placement, NullsOrdering.FIRST or NullsOrdering.LAST

None

Returns:

Type Description
ormar.queryset.actions.OrderGroup

OrderGroup for operator

Raises:

Type Description
ValueError

if nulls_ordering is not a NullsOrdering member

Source code in ormar/queryset/field_accessor.py
Python
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def desc(self, nulls_ordering: Optional[NullsOrdering] = None) -> OrderAction:
    """
    works as sql `column desc`

    :param nulls_ordering: optional nulls placement, `NullsOrdering.FIRST`
        or `NullsOrdering.LAST`
    :type nulls_ordering: Optional[NullsOrdering]
    :raises ValueError: if `nulls_ordering` is not a `NullsOrdering` member
    :return: OrderGroup for operator
    :rtype: ormar.queryset.actions.OrderGroup
    """
    nulls_value = self._coerce_nulls_ordering(nulls_ordering)
    return OrderAction(
        order_str="-" + self._access_chain,
        model_cls=self._source_model,
        nulls_ordering=nulls_value,
    )

endswith(other)

works as sql column LIKE '%<VALUE>'

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
270
271
272
273
274
275
276
277
278
279
def endswith(self, other: Any) -> FilterGroup:
    """
    works as sql `column LIKE '%<VALUE>'`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="endswith", other=other)

icontains(other)

works as sql column LIKE '%<VALUE>%' case-insensitive

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
237
238
239
240
241
242
243
244
245
246
def icontains(self, other: Any) -> FilterGroup:
    """
    works as sql `column LIKE '%<VALUE>%'` case-insensitive

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="icontains", other=other)

iendswith(other)

works as sql column LIKE '%<VALUE>' case-insensitive

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
281
282
283
284
285
286
287
288
289
290
def iendswith(self, other: Any) -> FilterGroup:
    """
    works as sql `column LIKE '%<VALUE>'` case-insensitive

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="iendswith", other=other)

iexact(other)

works as sql column = <VALUE> case-insensitive

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
215
216
217
218
219
220
221
222
223
224
def iexact(self, other: Any) -> FilterGroup:
    """
    works as sql `column = <VALUE>` case-insensitive

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="iexact", other=other)

in_(other)

works as sql column IN (<VALUE1>, <VALUE2>,...)

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
204
205
206
207
208
209
210
211
212
213
def in_(self, other: Any) -> FilterGroup:
    """
    works as sql `column IN (<VALUE1>, <VALUE2>,...)`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="in", other=other)

isnull(other)

works as sql column IS NULL or IS NOT NULL

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
292
293
294
295
296
297
298
299
300
301
def isnull(self, other: Any) -> FilterGroup:
    """
    works as sql `column IS NULL` or `IS NOT NULL`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="isnull", other=other)

istartswith(other)

works as sql column LIKE '%<VALUE>' case-insensitive

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
259
260
261
262
263
264
265
266
267
268
def istartswith(self, other: Any) -> FilterGroup:
    """
    works as sql `column LIKE '%<VALUE>'` case-insensitive

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="istartswith", other=other)

startswith(other)

works as sql column LIKE '<VALUE>%'

Parameters:

Name Type Description Default
other Any

value to check agains operator

required

Returns:

Type Description
ormar.queryset.clause.FilterGroup

FilterGroup for operator

Source code in ormar/queryset/field_accessor.py
Python
248
249
250
251
252
253
254
255
256
257
def startswith(self, other: Any) -> FilterGroup:
    """
    works as sql `column LIKE '<VALUE>%'`

    :param other: value to check agains operator
    :type other: str
    :return: FilterGroup for operator
    :rtype: ormar.queryset.clause.FilterGroup
    """
    return self._select_operator(op="startswith", other=other)

key_accessor(source_model, model, prefix)

Accessor for the primary key of model reached from source_model through prefix, used by Model.pk and Model.relation.pk.

A single column key resolves to the accessor of its field. A composite key resolves to a key accessor whose == and in_() / << filters compile to row-value comparisons like filter(pk=...) and filter(pk__in=...).

Parameters:

Name Type Description Default
source_model type[Model]

model the filter is built for

required
model type[Model]

model owning the primary key

required
prefix str

relation access chain leading to model, "" for the source model itself

required

Returns:

Type Description
FieldAccessor

accessor of the key

Source code in ormar/queryset/field_accessor.py
Python
350
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
def key_accessor(
    source_model: type["Model"], model: type["Model"], prefix: str
) -> FieldAccessor:
    """
    Accessor for the primary key of ``model`` reached from ``source_model``
    through ``prefix``, used by ``Model.pk`` and ``Model.relation.pk``.

    A single column key resolves to the accessor of its field. A composite key
    resolves to a key accessor whose ``==`` and ``in_()`` / ``<<`` filters
    compile to row-value comparisons like ``filter(pk=...)`` and
    ``filter(pk__in=...)``.

    :param source_model: model the filter is built for
    :type source_model: type[Model]
    :param model: model owning the primary key
    :type model: type[Model]
    :param prefix: relation access chain leading to ``model``, ``""`` for the
        source model itself
    :type prefix: str
    :return: accessor of the key
    :rtype: FieldAccessor
    """
    key = model.ormar_config.pk
    if key.is_composite:
        return FieldAccessor(
            source_model=source_model, access_chain=f"{prefix}pk", is_key=True
        )
    field = model.ormar_config.model_fields[key.fields[0]]
    return FieldAccessor(
        source_model=source_model, field=field, access_chain=prefix + field.name
    )