Skip to content

field_accessor

FieldAccessor

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

Source code in ormar\queryset\field_accessor.py
 11
 12
 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
class FieldAccessor:
    """
    Helper to access ormar fields directly from Model class also for nested
    models attributes.
    """

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

    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 (
            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 not self._field:
            raise AttributeError(
                "Cannot filter by Model, you need to provide model name"
            )

    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 agains operator
        :type other: str
        :return: FilterGroup for operator
        :rtype: ormar.queryset.clause.FilterGroup
        """
        return self._select_operator(op="__eq__", 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) -> OrderAction:
        """
        works as sql `column asc`

        :return: OrderGroup for operator
        :rtype: ormar.queryset.actions.OrderGroup
        """
        return OrderAction(order_str=self._access_chain, model_cls=self._source_model)

    def desc(self) -> OrderAction:
        """
        works as sql `column desc`

        :return: OrderGroup for operator
        :rtype: ormar.queryset.actions.OrderGroup
        """
        return OrderAction(
            order_str="-" + self._access_chain, model_cls=self._source_model
        )

__eq__(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
76
77
78
79
80
81
82
83
84
85
def __eq__(self, other: Any) -> FilterGroup:  # type: ignore
    """
    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="__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
87
88
89
90
91
92
93
94
95
96
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
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
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 (
        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
 98
 99
100
101
102
103
104
105
106
107
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
109
110
111
112
113
114
115
116
117
118
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
142
143
144
145
146
147
148
149
150
151
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
120
121
122
123
124
125
126
127
128
129
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
131
132
133
134
135
136
137
138
139
140
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)

__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
153
154
155
156
157
158
159
160
161
162
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()

works as sql column asc

Returns:

Type Description
ormar.queryset.actions.OrderGroup

OrderGroup for operator

Source code in ormar\queryset\field_accessor.py
263
264
265
266
267
268
269
270
def asc(self) -> OrderAction:
    """
    works as sql `column asc`

    :return: OrderGroup for operator
    :rtype: ormar.queryset.actions.OrderGroup
    """
    return OrderAction(order_str=self._access_chain, model_cls=self._source_model)

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
186
187
188
189
190
191
192
193
194
195
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()

works as sql column desc

Returns:

Type Description
ormar.queryset.actions.OrderGroup

OrderGroup for operator

Source code in ormar\queryset\field_accessor.py
272
273
274
275
276
277
278
279
280
281
def desc(self) -> OrderAction:
    """
    works as sql `column desc`

    :return: OrderGroup for operator
    :rtype: ormar.queryset.actions.OrderGroup
    """
    return OrderAction(
        order_str="-" + self._access_chain, model_cls=self._source_model
    )

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
230
231
232
233
234
235
236
237
238
239
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
197
198
199
200
201
202
203
204
205
206
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
241
242
243
244
245
246
247
248
249
250
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
175
176
177
178
179
180
181
182
183
184
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
164
165
166
167
168
169
170
171
172
173
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
252
253
254
255
256
257
258
259
260
261
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
219
220
221
222
223
224
225
226
227
228
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
208
209
210
211
212
213
214
215
216
217
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)