Skip to content

querysetproxy

QuerysetProxy

Bases: Generic[T]

Exposes QuerySet methods on relations, but also handles creating and removing of through Models for m2m relations.

Source code in ormar/relations/querysetproxy.py
 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
348
349
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
class QuerysetProxy(Generic[T]):
    """
    Exposes QuerySet methods on relations, but also handles creating and removing
    of through Models for m2m relations.
    """

    if TYPE_CHECKING:  # pragma no cover
        relation: "Relation"

    def __init__(
        self,
        relation: "Relation",
        to: Type["T"],
        type_: "RelationType",
        qryset: "QuerySet[T]" = None,
    ) -> None:
        self.relation: "Relation" = relation
        self._queryset: Optional["QuerySet[T]"] = qryset
        self.type_: "RelationType" = type_
        self._owner: Union[CallableProxyType, "Model"] = self.relation.manager.owner
        self.related_field_name = self._owner.Meta.model_fields[
            self.relation.field_name
        ].get_related_name()
        self.to: Type[T] = to
        self.related_field = to.Meta.model_fields[self.related_field_name]
        self.owner_pk_value = self._owner.pk
        self.through_model_name = (
            self.related_field.through.get_name()
            if self.type_ == ormar.RelationType.MULTIPLE
            else ""
        )

    @property
    def queryset(self) -> "QuerySet[T]":
        """
        Returns queryset if it's set, AttributeError otherwise.
        :return: QuerySet
        :rtype: QuerySet
        """
        if not self._queryset:
            raise AttributeError
        return self._queryset

    @queryset.setter
    def queryset(self, value: "QuerySet") -> None:
        """
        Set's the queryset. Initialized in RelationProxy.
        :param value: QuerySet
        :type value: QuerySet
        """
        self._queryset = value

    def _assign_child_to_parent(self, child: Optional["T"]) -> None:
        """
        Registers child in parents RelationManager.

        :param child: child to register on parent side.
        :type child: Model
        """
        if child:
            owner = self._owner
            rel_name = self.relation.field_name
            setattr(owner, rel_name, child)

    def _register_related(self, child: Union["T", Sequence[Optional["T"]]]) -> None:
        """
        Registers child/ children in parents RelationManager.

        :param child: child or list of children models to register.
        :type child: Union[Model,List[Model]]
        """
        if isinstance(child, list):
            for subchild in child:
                self._assign_child_to_parent(subchild)
        else:
            assert isinstance(child, ormar.Model)
            child = cast("T", child)
            self._assign_child_to_parent(child)

    def _clean_items_on_load(self) -> None:
        """
        Cleans the current list of the related models.
        """
        if isinstance(self.relation.related_models, MutableSequence):
            for item in self.relation.related_models[:]:
                self.relation.remove(item)

    async def create_through_instance(self, child: "T", **kwargs: Any) -> None:
        """
        Crete a through model instance in the database for m2m relations.

        :param kwargs: dict of additional keyword arguments for through instance
        :type kwargs: Any
        :param child: child model instance
        :type child: Model
        """
        model_cls = self.relation.through
        owner_column = self.related_field.default_target_field_name()  # type: ignore
        child_column = self.related_field.default_source_field_name()  # type: ignore
        rel_kwargs = {owner_column: self._owner.pk, child_column: child.pk}
        final_kwargs = {**rel_kwargs, **kwargs}
        if child.pk is None:
            raise ModelPersistenceError(
                f"You cannot save {child.get_name()} "
                f"model without primary key set! \n"
                f"Save the child model first."
            )
        await model_cls(**final_kwargs).save()

    async def update_through_instance(self, child: "T", **kwargs: Any) -> None:
        """
        Updates a through model instance in the database for m2m relations.

        :param kwargs: dict of additional keyword arguments for through instance
        :type kwargs: Any
        :param child: child model instance
        :type child: Model
        """
        model_cls = self.relation.through
        owner_column = self.related_field.default_target_field_name()  # type: ignore
        child_column = self.related_field.default_source_field_name()  # type: ignore
        rel_kwargs = {owner_column: self._owner.pk, child_column: child.pk}
        through_model = await model_cls.objects.get(**rel_kwargs)
        await through_model.update(**kwargs)

    async def upsert_through_instance(self, child: "T", **kwargs: Any) -> None:
        """
        Updates a through model instance in the database for m2m relations if
        it already exists, else creates one.

        :param kwargs: dict of additional keyword arguments for through instance
        :type kwargs: Any
        :param child: child model instance
        :type child: Model
        """
        try:
            await self.update_through_instance(child=child, **kwargs)
        except NoMatch:
            await self.create_through_instance(child=child, **kwargs)

    async def delete_through_instance(self, child: "T") -> None:
        """
        Removes through model instance from the database for m2m relations.

        :param child: child model instance
        :type child: Model
        """
        queryset = ormar.QuerySet(model_cls=self.relation.through)  # type: ignore
        owner_column = self.related_field.default_target_field_name()  # type: ignore
        child_column = self.related_field.default_source_field_name()  # type: ignore
        kwargs = {owner_column: self._owner, child_column: child}
        link_instance = await queryset.filter(**kwargs).get()  # type: ignore
        await link_instance.delete()

    async def exists(self) -> bool:
        """
        Returns a bool value to confirm if there are rows matching the given criteria
        (applied with `filter` and `exclude` if set).

        Actual call delegated to QuerySet.

        :return: result of the check
        :rtype: bool
        """
        return await self.queryset.exists()

    async def count(self, distinct: bool = True) -> int:
        """
        Returns number of rows matching the given criteria
        (applied with `filter` and `exclude` if set before).
        If `distinct` is `True` (the default), this will return
        the number of primary rows selected. If `False`,
        the count will be the total number of rows returned
        (including extra rows for `one-to-many` or `many-to-many`
        left `select_related` table joins).
        `False` is the legacy (buggy) behavior for workflows that depend on it.

        Actual call delegated to QuerySet.

        :param distinct: flag if the primary table rows should be distinct or not
        :return: number of rows
        :rtype: int
        """
        return await self.queryset.count(distinct=distinct)

    async def max(self, columns: Union[str, List[str]]) -> Any:  # noqa: A003
        """
        Returns max value of columns for rows matching the given criteria
        (applied with `filter` and `exclude` if set before).

        :return: max value of column(s)
        :rtype: Any
        """
        return await self.queryset.max(columns=columns)

    async def min(self, columns: Union[str, List[str]]) -> Any:  # noqa: A003
        """
        Returns min value of columns for rows matching the given criteria
        (applied with `filter` and `exclude` if set before).

        :return: min value of column(s)
        :rtype: Any
        """
        return await self.queryset.min(columns=columns)

    async def sum(self, columns: Union[str, List[str]]) -> Any:  # noqa: A003
        """
        Returns sum value of columns for rows matching the given criteria
        (applied with `filter` and `exclude` if set before).

        :return: sum value of columns
        :rtype: int
        """
        return await self.queryset.sum(columns=columns)

    async def avg(self, columns: Union[str, List[str]]) -> Any:
        """
        Returns avg value of columns for rows matching the given criteria
        (applied with `filter` and `exclude` if set before).

        :return: avg value of columns
        :rtype: Union[int, float, List]
        """
        return await self.queryset.avg(columns=columns)

    async def clear(self, keep_reversed: bool = True) -> int:
        """
        Removes all related models from given relation.

        Removes all through models for m2m relation.

        For reverse FK relations keep_reversed flag marks if the reversed models
        should be kept or deleted from the database too (False means that models
        will be deleted, and not only removed from relation).

        :param keep_reversed: flag if reverse models in reverse FK should be deleted
        or not, keep_reversed=False deletes them from database.
        :type keep_reversed: bool
        :return: number of deleted models
        :rtype: int
        """
        if self.type_ == ormar.RelationType.MULTIPLE:
            queryset = ormar.QuerySet(model_cls=self.relation.through)  # type: ignore
            owner_column = self._owner.get_name()
        else:
            queryset = ormar.QuerySet(model_cls=self.relation.to)  # type: ignore
            owner_column = self.related_field_name
        kwargs = {owner_column: self._owner}
        self._clean_items_on_load()
        if keep_reversed and self.type_ == ormar.RelationType.REVERSE:
            update_kwrgs = {f"{owner_column}": None}
            return await queryset.filter(_exclude=False, **kwargs).update(
                each=False, **update_kwrgs
            )
        return await queryset.delete(**kwargs)  # type: ignore

    async def values(
        self, fields: Union[List, str, Set, Dict] = None, exclude_through: bool = False
    ) -> List:
        """
        Return a list of dictionaries with column values in order of the fields
        passed or all fields from queried models.

        To filter for given row use filter/exclude methods before values,
        to limit number of rows use limit/offset or paginate before values.

        Note that it always return a list even for one row from database.

        :param exclude_through: flag if through models should be excluded
        :type exclude_through: bool
        :param fields: field name or list of field names to extract from db
        :type fields:  Union[List, str, Set, Dict]
        """
        return await self.queryset.values(
            fields=fields, exclude_through=exclude_through
        )

    async def values_list(
        self,
        fields: Union[List, str, Set, Dict] = None,
        flatten: bool = False,
        exclude_through: bool = False,
    ) -> List:
        """
        Return a list of tuples with column values in order of the fields passed or
        all fields from queried models.

        When one field is passed you can flatten the list of tuples into list of values
        of that single field.

        To filter for given row use filter/exclude methods before values,
        to limit number of rows use limit/offset or paginate before values.

        Note that it always return a list even for one row from database.

        :param exclude_through: flag if through models should be excluded
        :type exclude_through: bool
        :param fields: field name or list of field names to extract from db
        :type fields: Union[str, List[str]]
        :param flatten: when one field is passed you can flatten the list of tuples
        :type flatten: bool
        """
        return await self.queryset.values(
            fields=fields,
            exclude_through=exclude_through,
            _as_dict=False,
            _flatten=flatten,
        )

    async def first(self, *args: Any, **kwargs: Any) -> "T":
        """
        Gets the first row from the db ordered by primary key column ascending.

        Actual call delegated to QuerySet.

        Passing args and/or kwargs is a shortcut and equals to calling
        `filter(*args, **kwargs).first()`.

        List of related models is cleared before the call.

        :param kwargs:
        :type kwargs:
        :return:
        :rtype: _asyncio.Future
        """
        first = await self.queryset.first(*args, **kwargs)
        self._clean_items_on_load()
        self._register_related(first)
        return first

    async def get_or_none(self, *args: Any, **kwargs: Any) -> Optional["T"]:
        """
        Get's the first row from the db meeting the criteria set by kwargs.

        If no criteria set it will return the last row in db sorted by pk.

        Passing args and/or kwargs is a shortcut and equals to calling
        `filter(*args, **kwargs).get_or_none()`.

        If not match is found None will be returned.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: returned model
        :rtype: Model
        """
        try:
            get = await self.queryset.get(*args, **kwargs)
        except ormar.NoMatch:
            return None

        self._clean_items_on_load()
        self._register_related(get)
        return get

    async def get(self, *args: Any, **kwargs: Any) -> "T":
        """
        Get's the first row from the db meeting the criteria set by kwargs.

        If no criteria set it will return the last row in db sorted by pk.

        Passing args and/or kwargs is a shortcut and equals to calling
        `filter(*args, **kwargs).get()`.

        Actual call delegated to QuerySet.

        List of related models is cleared before the call.

        :raises NoMatch: if no rows are returned
        :raises MultipleMatches: if more than 1 row is returned.
        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: returned model
        :rtype: Model
        """
        get = await self.queryset.get(*args, **kwargs)
        self._clean_items_on_load()
        self._register_related(get)
        return get

    async def all(self, *args: Any, **kwargs: Any) -> List["T"]:  # noqa: A003
        """
        Returns all rows from a database for given model for set filter options.

        Passing args and/or kwargs is a shortcut and equals to calling
        `filter(*args, **kwargs).all()`.

        If there are no rows meeting the criteria an empty list is returned.

        Actual call delegated to QuerySet.

        List of related models is cleared before the call.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: list of returned models
        :rtype: List[Model]
        """
        all_items = await self.queryset.all(*args, **kwargs)
        self._clean_items_on_load()
        self._register_related(all_items)
        return all_items

    async def create(self, **kwargs: Any) -> "T":
        """
        Creates the model instance, saves it in a database and returns the updates model
        (with pk populated if not passed and autoincrement is set).

        The allowed kwargs are `Model` fields names and proper value types.

        For m2m relation the through model is created automatically.

        Actual call delegated to QuerySet.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: created model
        :rtype: Model
        """
        through_kwargs = kwargs.pop(self.through_model_name, {})
        if self.type_ == ormar.RelationType.REVERSE:
            kwargs[self.related_field_name] = self._owner
        created = await self.queryset.create(**kwargs)
        self._register_related(created)
        if self.type_ == ormar.RelationType.MULTIPLE:
            await self.create_through_instance(created, **through_kwargs)
        return created

    async def update(self, each: bool = False, **kwargs: Any) -> int:
        """
        Updates the model table after applying the filters from kwargs.

        You have to either pass a filter to narrow down a query or explicitly pass
        each=True flag to affect whole table.

        :param each: flag if whole table should be affected if no filter is passed
        :type each: bool
        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: number of updated rows
        :rtype: int
        """
        # queryset proxy always have one filter for pk of parent model
        if (
            not each
            and (len(self.queryset.filter_clauses) + len(self.queryset.exclude_clauses))
            == 1
        ):
            raise QueryDefinitionError(
                "You cannot update without filtering the queryset first. "
                "If you want to update all rows use update(each=True, **kwargs)"
            )

        through_kwargs = kwargs.pop(self.through_model_name, {})
        children = await self.queryset.all()
        for child in children:
            await child.update(**kwargs)  # type: ignore
            if self.type_ == ormar.RelationType.MULTIPLE and through_kwargs:
                await self.update_through_instance(
                    child=child, **through_kwargs  # type: ignore
                )
        return len(children)

    async def get_or_create(
        self,
        _defaults: Optional[Dict[str, Any]] = None,
        *args: Any,
        **kwargs: Any,
    ) -> Tuple["T", bool]:
        """
        Combination of create and get methods.

        Tries to get a row meeting the criteria fro kwargs
        and if `NoMatch` exception is raised
        it creates a new one with given kwargs and _defaults.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :param _defaults: default values for creating object
        :type _defaults: Optional[Dict[str, Any]]
        :return: model instance and a boolean
        :rtype: Tuple("T", bool)
        """
        try:
            return await self.get(*args, **kwargs), False
        except NoMatch:
            _defaults = _defaults or {}
            return await self.create(**{**kwargs, **_defaults}), True

    async def update_or_create(self, **kwargs: Any) -> "T":
        """
        Updates the model, or in case there is no match in database creates a new one.

        Actual call delegated to QuerySet.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: updated or created model
        :rtype: Model
        """
        pk_name = self.queryset.model_meta.pkname
        if "pk" in kwargs:
            kwargs[pk_name] = kwargs.pop("pk")
        if pk_name not in kwargs or kwargs.get(pk_name) is None:
            return await self.create(**kwargs)
        model = await self.queryset.get(pk=kwargs[pk_name])
        return await model.update(**kwargs)

    def filter(  # noqa: A003, A001
        self, *args: Any, **kwargs: Any
    ) -> "QuerysetProxy[T]":
        """
        Allows you to filter by any `Model` attribute/field
        as well as to fetch instances, with a filter across an FK relationship.

        You can use special filter suffix to change the filter operands:

        *  exact - like `album__name__exact='Malibu'` (exact match)
        *  iexact - like `album__name__iexact='malibu'` (exact match case insensitive)
        *  contains - like `album__name__contains='Mal'` (sql like)
        *  icontains - like `album__name__icontains='mal'` (sql like case insensitive)
        *  in - like `album__name__in=['Malibu', 'Barclay']` (sql in)
        *  isnull - like `album__name__isnull=True` (sql is null)
           (isnotnull `album__name__isnull=False` (sql is not null))
        *  gt - like `position__gt=3` (sql >)
        *  gte - like `position__gte=3` (sql >=)
        *  lt - like `position__lt=3` (sql <)
        *  lte - like `position__lte=3` (sql <=)
        *  startswith - like `album__name__startswith='Mal'` (exact start match)
        *  istartswith - like `album__name__istartswith='mal'` (case insensitive)
        *  endswith - like `album__name__endswith='ibu'` (exact end match)
        *  iendswith - like `album__name__iendswith='IBU'` (case insensitive)

        Actual call delegated to QuerySet.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: filtered QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.filter(*args, **kwargs)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def exclude(
        self, *args: Any, **kwargs: Any
    ) -> "QuerysetProxy[T]":  # noqa: A003, A001
        """
        Works exactly the same as filter and all modifiers (suffixes) are the same,
        but returns a *not* condition.

        So if you use `filter(name='John')` which is `where name = 'John'` in SQL,
        the `exclude(name='John')` equals to `where name <> 'John'`

        Note that all conditions are joined so if you pass multiple values it
        becomes a union of conditions.

        `exclude(name='John', age>=35)` will become
        `where not (name='John' and age>=35)`

        Actual call delegated to QuerySet.

        :param kwargs: fields names and proper value types
        :type kwargs: Any
        :return: filtered QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.exclude(*args, **kwargs)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def select_all(self, follow: bool = False) -> "QuerysetProxy[T]":
        """
        By default adds only directly related models.

        If follow=True is set it adds also related models of related models.

        To not get stuck in an infinite loop as related models also keep a relation
        to parent model visited models set is kept.

        That way already visited models that are nested are loaded, but the load do not
        follow them inside. So Model A -> Model B -> Model C -> Model A -> Model X
        will load second Model A but will never follow into Model X.
        Nested relations of those kind need to be loaded manually.

        :param follow: flag to trigger deep save -
        by default only directly related models are saved
        with follow=True also related models of related models are saved
        :type follow: bool
        :return: reloaded Model
        :rtype: Model
        """
        queryset = self.queryset.select_all(follow=follow)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def select_related(self, related: Union[List, str]) -> "QuerysetProxy[T]":
        """
        Allows to prefetch related models during the same query.

        **With `select_related` always only one query is run against the database**,
        meaning that one (sometimes complicated) join is generated and later nested
        models are processed in python.

        To fetch related model use `ForeignKey` names.

        To chain related `Models` relation use double underscores between names.

        Actual call delegated to QuerySet.

        :param related: list of relation field names, can be linked by '__' to nest
        :type related: Union[List, str]
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.select_related(related)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def prefetch_related(self, related: Union[List, str]) -> "QuerysetProxy[T]":
        """
        Allows to prefetch related models during query - but opposite to
        `select_related` each subsequent model is fetched in a separate database query.

        **With `prefetch_related` always one query per Model is run against the
        database**, meaning that you will have multiple queries executed one
        after another.

        To fetch related model use `ForeignKey` names.

        To chain related `Models` relation use double underscores between names.

        Actual call delegated to QuerySet.

        :param related: list of relation field names, can be linked by '__' to nest
        :type related: Union[List, str]
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.prefetch_related(related)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def paginate(self, page: int, page_size: int = 20) -> "QuerysetProxy[T]":
        """
        You can paginate the result which is a combination of offset and limit clauses.
        Limit is set to page size and offset is set to (page-1) * page_size.

        Actual call delegated to QuerySet.

        :param page_size: numbers of items per page
        :type page_size: int
        :param page: page number
        :type page: int
        :return: QuerySet
        :rtype: QuerySet
        """
        queryset = self.queryset.paginate(page=page, page_size=page_size)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def limit(self, limit_count: int) -> "QuerysetProxy[T]":
        """
        You can limit the results to desired number of parent models.

        Actual call delegated to QuerySet.

        :param limit_count: number of models to limit
        :type limit_count: int
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.limit(limit_count)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def offset(self, offset: int) -> "QuerysetProxy[T]":
        """
        You can also offset the results by desired number of main models.

        Actual call delegated to QuerySet.

        :param offset: numbers of models to offset
        :type offset: int
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.offset(offset)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def fields(self, columns: Union[List, str, Set, Dict]) -> "QuerysetProxy[T]":
        """
        With `fields()` you can select subset of model columns to limit the data load.

        Note that `fields()` and `exclude_fields()` works both for main models
        (on normal queries like `get`, `all` etc.)
        as well as `select_related` and `prefetch_related`
        models (with nested notation).

        You can select specified fields by passing a `str, List[str], Set[str] or
        dict` with nested definition.

        To include related models use notation
        `{related_name}__{column}[__{optional_next} etc.]`.

        `fields()` can be called several times, building up the columns to select.

        If you include related models into `select_related()` call but you won't specify
        columns for those models in fields - implies a list of all fields for
        those nested models.

        Mandatory fields cannot be excluded as it will raise `ValidationError`,
         to exclude a field it has to be nullable.

        Pk column cannot be excluded - it's always auto added even if
        not explicitly included.

        You can also pass fields to include as dictionary or set.

        To mark a field as included in a dictionary use it's name as key
        and ellipsis as value.

        To traverse nested models use nested dictionaries.

        To include fields at last level instead of nested dictionary a set can be used.

        To include whole nested model specify model related field name and ellipsis.

        Actual call delegated to QuerySet.

        :param columns: columns to include
        :type columns: Union[List, str, Set, Dict]
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.fields(columns)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def exclude_fields(
        self, columns: Union[List, str, Set, Dict]
    ) -> "QuerysetProxy[T]":
        """
        With `exclude_fields()` you can select subset of model columns that will
        be excluded to limit the data load.

        It's the opposite of `fields()` method so check documentation above
        to see what options are available.

        Especially check above how you can pass also nested dictionaries
        and sets as a mask to exclude fields from whole hierarchy.

        Note that `fields()` and `exclude_fields()` works both for main models
        (on normal queries like `get`, `all` etc.)
        as well as `select_related` and `prefetch_related` models
        (with nested notation).

        Mandatory fields cannot be excluded as it will raise `ValidationError`,
        to exclude a field it has to be nullable.

        Pk column cannot be excluded - it's always auto added even
        if explicitly excluded.

        Actual call delegated to QuerySet.

        :param columns: columns to exclude
        :type columns: Union[List, str, Set, Dict]
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.exclude_fields(columns=columns)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

    def order_by(self, columns: Union[List, str, "OrderAction"]) -> "QuerysetProxy[T]":
        """
        With `order_by()` you can order the results from database based on your
        choice of fields.

        You can provide a string with field name or list of strings with fields names.

        Ordering in sql will be applied in order of names you provide in order_by.

        By default if you do not provide ordering `ormar` explicitly orders by
        all primary keys

        If you are sorting by nested models that causes that the result rows are
        unsorted by the main model `ormar` will combine those children rows into
        one main model.

        The main model will never duplicate in the result

        To order by main model field just provide a field name

        To sort on nested models separate field names with dunder '__'.

        You can sort this way across all relation types -> `ForeignKey`,
        reverse virtual FK and `ManyToMany` fields.

        To sort in descending order provide a hyphen in front of the field name

        Actual call delegated to QuerySet.

        :param columns: columns by which models should be sorted
        :type columns: Union[List, str]
        :return: QuerysetProxy
        :rtype: QuerysetProxy
        """
        queryset = self.queryset.order_by(columns)
        return self.__class__(
            relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
        )

all(*args, **kwargs) async

Returns all rows from a database for given model for set filter options.

Passing args and/or kwargs is a shortcut and equals to calling filter(*args, **kwargs).all().

If there are no rows meeting the criteria an empty list is returned.

Actual call delegated to QuerySet.

List of related models is cleared before the call.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
List[Model]

list of returned models

Source code in ormar/relations/querysetproxy.py
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
async def all(self, *args: Any, **kwargs: Any) -> List["T"]:  # noqa: A003
    """
    Returns all rows from a database for given model for set filter options.

    Passing args and/or kwargs is a shortcut and equals to calling
    `filter(*args, **kwargs).all()`.

    If there are no rows meeting the criteria an empty list is returned.

    Actual call delegated to QuerySet.

    List of related models is cleared before the call.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: list of returned models
    :rtype: List[Model]
    """
    all_items = await self.queryset.all(*args, **kwargs)
    self._clean_items_on_load()
    self._register_related(all_items)
    return all_items

avg(columns) async

Returns avg value of columns for rows matching the given criteria (applied with filter and exclude if set before).

Returns:

Type Description
Union[int, float, List]

avg value of columns

Source code in ormar/relations/querysetproxy.py
246
247
248
249
250
251
252
253
254
async def avg(self, columns: Union[str, List[str]]) -> Any:
    """
    Returns avg value of columns for rows matching the given criteria
    (applied with `filter` and `exclude` if set before).

    :return: avg value of columns
    :rtype: Union[int, float, List]
    """
    return await self.queryset.avg(columns=columns)

clear(keep_reversed=True) async

Removes all related models from given relation.

Removes all through models for m2m relation.

For reverse FK relations keep_reversed flag marks if the reversed models should be kept or deleted from the database too (False means that models will be deleted, and not only removed from relation).

Parameters:

Name Type Description Default
keep_reversed bool

flag if reverse models in reverse FK should be deleted or not, keep_reversed=False deletes them from database.

True

Returns:

Type Description
int

number of deleted models

Source code in ormar/relations/querysetproxy.py
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
async def clear(self, keep_reversed: bool = True) -> int:
    """
    Removes all related models from given relation.

    Removes all through models for m2m relation.

    For reverse FK relations keep_reversed flag marks if the reversed models
    should be kept or deleted from the database too (False means that models
    will be deleted, and not only removed from relation).

    :param keep_reversed: flag if reverse models in reverse FK should be deleted
    or not, keep_reversed=False deletes them from database.
    :type keep_reversed: bool
    :return: number of deleted models
    :rtype: int
    """
    if self.type_ == ormar.RelationType.MULTIPLE:
        queryset = ormar.QuerySet(model_cls=self.relation.through)  # type: ignore
        owner_column = self._owner.get_name()
    else:
        queryset = ormar.QuerySet(model_cls=self.relation.to)  # type: ignore
        owner_column = self.related_field_name
    kwargs = {owner_column: self._owner}
    self._clean_items_on_load()
    if keep_reversed and self.type_ == ormar.RelationType.REVERSE:
        update_kwrgs = {f"{owner_column}": None}
        return await queryset.filter(_exclude=False, **kwargs).update(
            each=False, **update_kwrgs
        )
    return await queryset.delete(**kwargs)  # type: ignore

count(distinct=True) async

Returns number of rows matching the given criteria (applied with filter and exclude if set before). If distinct is True (the default), this will return the number of primary rows selected. If False, the count will be the total number of rows returned (including extra rows for one-to-many or many-to-many left select_related table joins). False is the legacy (buggy) behavior for workflows that depend on it.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
distinct bool

flag if the primary table rows should be distinct or not

True

Returns:

Type Description
int

number of rows

Source code in ormar/relations/querysetproxy.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
async def count(self, distinct: bool = True) -> int:
    """
    Returns number of rows matching the given criteria
    (applied with `filter` and `exclude` if set before).
    If `distinct` is `True` (the default), this will return
    the number of primary rows selected. If `False`,
    the count will be the total number of rows returned
    (including extra rows for `one-to-many` or `many-to-many`
    left `select_related` table joins).
    `False` is the legacy (buggy) behavior for workflows that depend on it.

    Actual call delegated to QuerySet.

    :param distinct: flag if the primary table rows should be distinct or not
    :return: number of rows
    :rtype: int
    """
    return await self.queryset.count(distinct=distinct)

create(**kwargs) async

Creates the model instance, saves it in a database and returns the updates model (with pk populated if not passed and autoincrement is set).

The allowed kwargs are Model fields names and proper value types.

For m2m relation the through model is created automatically.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
Model

created model

Source code in ormar/relations/querysetproxy.py
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
async def create(self, **kwargs: Any) -> "T":
    """
    Creates the model instance, saves it in a database and returns the updates model
    (with pk populated if not passed and autoincrement is set).

    The allowed kwargs are `Model` fields names and proper value types.

    For m2m relation the through model is created automatically.

    Actual call delegated to QuerySet.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: created model
    :rtype: Model
    """
    through_kwargs = kwargs.pop(self.through_model_name, {})
    if self.type_ == ormar.RelationType.REVERSE:
        kwargs[self.related_field_name] = self._owner
    created = await self.queryset.create(**kwargs)
    self._register_related(created)
    if self.type_ == ormar.RelationType.MULTIPLE:
        await self.create_through_instance(created, **through_kwargs)
    return created

create_through_instance(child, **kwargs) async

Crete a through model instance in the database for m2m relations.

Parameters:

Name Type Description Default
kwargs Any

dict of additional keyword arguments for through instance

required
child 'T'

child model instance

required
Source code in ormar/relations/querysetproxy.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
async def create_through_instance(self, child: "T", **kwargs: Any) -> None:
    """
    Crete a through model instance in the database for m2m relations.

    :param kwargs: dict of additional keyword arguments for through instance
    :type kwargs: Any
    :param child: child model instance
    :type child: Model
    """
    model_cls = self.relation.through
    owner_column = self.related_field.default_target_field_name()  # type: ignore
    child_column = self.related_field.default_source_field_name()  # type: ignore
    rel_kwargs = {owner_column: self._owner.pk, child_column: child.pk}
    final_kwargs = {**rel_kwargs, **kwargs}
    if child.pk is None:
        raise ModelPersistenceError(
            f"You cannot save {child.get_name()} "
            f"model without primary key set! \n"
            f"Save the child model first."
        )
    await model_cls(**final_kwargs).save()

delete_through_instance(child) async

Removes through model instance from the database for m2m relations.

Parameters:

Name Type Description Default
child 'T'

child model instance

required
Source code in ormar/relations/querysetproxy.py
171
172
173
174
175
176
177
178
179
180
181
182
183
async def delete_through_instance(self, child: "T") -> None:
    """
    Removes through model instance from the database for m2m relations.

    :param child: child model instance
    :type child: Model
    """
    queryset = ormar.QuerySet(model_cls=self.relation.through)  # type: ignore
    owner_column = self.related_field.default_target_field_name()  # type: ignore
    child_column = self.related_field.default_source_field_name()  # type: ignore
    kwargs = {owner_column: self._owner, child_column: child}
    link_instance = await queryset.filter(**kwargs).get()  # type: ignore
    await link_instance.delete()

exclude(*args, **kwargs)

Works exactly the same as filter and all modifiers (suffixes) are the same, but returns a not condition.

So if you use filter(name='John') which is where name = 'John' in SQL, the exclude(name='John') equals to where name <> 'John'

Note that all conditions are joined so if you pass multiple values it becomes a union of conditions.

exclude(name='John', age>=35) will become where not (name='John' and age>=35)

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
QuerysetProxy

filtered QuerysetProxy

Source code in ormar/relations/querysetproxy.py
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
def exclude(
    self, *args: Any, **kwargs: Any
) -> "QuerysetProxy[T]":  # noqa: A003, A001
    """
    Works exactly the same as filter and all modifiers (suffixes) are the same,
    but returns a *not* condition.

    So if you use `filter(name='John')` which is `where name = 'John'` in SQL,
    the `exclude(name='John')` equals to `where name <> 'John'`

    Note that all conditions are joined so if you pass multiple values it
    becomes a union of conditions.

    `exclude(name='John', age>=35)` will become
    `where not (name='John' and age>=35)`

    Actual call delegated to QuerySet.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: filtered QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.exclude(*args, **kwargs)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

exclude_fields(columns)

With exclude_fields() you can select subset of model columns that will be excluded to limit the data load.

It's the opposite of fields() method so check documentation above to see what options are available.

Especially check above how you can pass also nested dictionaries and sets as a mask to exclude fields from whole hierarchy.

Note that fields() and exclude_fields() works both for main models (on normal queries like get, all etc.) as well as select_related and prefetch_related models (with nested notation).

Mandatory fields cannot be excluded as it will raise ValidationError, to exclude a field it has to be nullable.

Pk column cannot be excluded - it's always auto added even if explicitly excluded.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
columns Union[List, str, Set, Dict]

columns to exclude

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
def exclude_fields(
    self, columns: Union[List, str, Set, Dict]
) -> "QuerysetProxy[T]":
    """
    With `exclude_fields()` you can select subset of model columns that will
    be excluded to limit the data load.

    It's the opposite of `fields()` method so check documentation above
    to see what options are available.

    Especially check above how you can pass also nested dictionaries
    and sets as a mask to exclude fields from whole hierarchy.

    Note that `fields()` and `exclude_fields()` works both for main models
    (on normal queries like `get`, `all` etc.)
    as well as `select_related` and `prefetch_related` models
    (with nested notation).

    Mandatory fields cannot be excluded as it will raise `ValidationError`,
    to exclude a field it has to be nullable.

    Pk column cannot be excluded - it's always auto added even
    if explicitly excluded.

    Actual call delegated to QuerySet.

    :param columns: columns to exclude
    :type columns: Union[List, str, Set, Dict]
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.exclude_fields(columns=columns)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

exists() async

Returns a bool value to confirm if there are rows matching the given criteria (applied with filter and exclude if set).

Actual call delegated to QuerySet.

Returns:

Type Description
bool

result of the check

Source code in ormar/relations/querysetproxy.py
185
186
187
188
189
190
191
192
193
194
195
async def exists(self) -> bool:
    """
    Returns a bool value to confirm if there are rows matching the given criteria
    (applied with `filter` and `exclude` if set).

    Actual call delegated to QuerySet.

    :return: result of the check
    :rtype: bool
    """
    return await self.queryset.exists()

fields(columns)

With fields() you can select subset of model columns to limit the data load.

Note that fields() and exclude_fields() works both for main models (on normal queries like get, all etc.) as well as select_related and prefetch_related models (with nested notation).

You can select specified fields by passing a str, List[str], Set[str] or dict with nested definition.

To include related models use notation {related_name}__{column}[__{optional_next} etc.].

fields() can be called several times, building up the columns to select.

If you include related models into select_related() call but you won't specify columns for those models in fields - implies a list of all fields for those nested models.

Mandatory fields cannot be excluded as it will raise ValidationError, to exclude a field it has to be nullable.

Pk column cannot be excluded - it's always auto added even if not explicitly included.

You can also pass fields to include as dictionary or set.

To mark a field as included in a dictionary use it's name as key and ellipsis as value.

To traverse nested models use nested dictionaries.

To include fields at last level instead of nested dictionary a set can be used.

To include whole nested model specify model related field name and ellipsis.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
columns Union[List, str, Set, Dict]

columns to include

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
def fields(self, columns: Union[List, str, Set, Dict]) -> "QuerysetProxy[T]":
    """
    With `fields()` you can select subset of model columns to limit the data load.

    Note that `fields()` and `exclude_fields()` works both for main models
    (on normal queries like `get`, `all` etc.)
    as well as `select_related` and `prefetch_related`
    models (with nested notation).

    You can select specified fields by passing a `str, List[str], Set[str] or
    dict` with nested definition.

    To include related models use notation
    `{related_name}__{column}[__{optional_next} etc.]`.

    `fields()` can be called several times, building up the columns to select.

    If you include related models into `select_related()` call but you won't specify
    columns for those models in fields - implies a list of all fields for
    those nested models.

    Mandatory fields cannot be excluded as it will raise `ValidationError`,
     to exclude a field it has to be nullable.

    Pk column cannot be excluded - it's always auto added even if
    not explicitly included.

    You can also pass fields to include as dictionary or set.

    To mark a field as included in a dictionary use it's name as key
    and ellipsis as value.

    To traverse nested models use nested dictionaries.

    To include fields at last level instead of nested dictionary a set can be used.

    To include whole nested model specify model related field name and ellipsis.

    Actual call delegated to QuerySet.

    :param columns: columns to include
    :type columns: Union[List, str, Set, Dict]
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.fields(columns)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

filter(*args, **kwargs)

Allows you to filter by any Model attribute/field as well as to fetch instances, with a filter across an FK relationship.

You can use special filter suffix to change the filter operands:

  • exact - like album__name__exact='Malibu' (exact match)
  • iexact - like album__name__iexact='malibu' (exact match case insensitive)
  • contains - like album__name__contains='Mal' (sql like)
  • icontains - like album__name__icontains='mal' (sql like case insensitive)
  • in - like album__name__in=['Malibu', 'Barclay'] (sql in)
  • isnull - like album__name__isnull=True (sql is null) (isnotnull album__name__isnull=False (sql is not null))
  • gt - like position__gt=3 (sql >)
  • gte - like position__gte=3 (sql >=)
  • lt - like position__lt=3 (sql <)
  • lte - like position__lte=3 (sql <=)
  • startswith - like album__name__startswith='Mal' (exact start match)
  • istartswith - like album__name__istartswith='mal' (case insensitive)
  • endswith - like album__name__endswith='ibu' (exact end match)
  • iendswith - like album__name__iendswith='IBU' (case insensitive)

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
QuerysetProxy

filtered QuerysetProxy

Source code in ormar/relations/querysetproxy.py
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
def filter(  # noqa: A003, A001
    self, *args: Any, **kwargs: Any
) -> "QuerysetProxy[T]":
    """
    Allows you to filter by any `Model` attribute/field
    as well as to fetch instances, with a filter across an FK relationship.

    You can use special filter suffix to change the filter operands:

    *  exact - like `album__name__exact='Malibu'` (exact match)
    *  iexact - like `album__name__iexact='malibu'` (exact match case insensitive)
    *  contains - like `album__name__contains='Mal'` (sql like)
    *  icontains - like `album__name__icontains='mal'` (sql like case insensitive)
    *  in - like `album__name__in=['Malibu', 'Barclay']` (sql in)
    *  isnull - like `album__name__isnull=True` (sql is null)
       (isnotnull `album__name__isnull=False` (sql is not null))
    *  gt - like `position__gt=3` (sql >)
    *  gte - like `position__gte=3` (sql >=)
    *  lt - like `position__lt=3` (sql <)
    *  lte - like `position__lte=3` (sql <=)
    *  startswith - like `album__name__startswith='Mal'` (exact start match)
    *  istartswith - like `album__name__istartswith='mal'` (case insensitive)
    *  endswith - like `album__name__endswith='ibu'` (exact end match)
    *  iendswith - like `album__name__iendswith='IBU'` (case insensitive)

    Actual call delegated to QuerySet.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: filtered QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.filter(*args, **kwargs)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

first(*args, **kwargs) async

Gets the first row from the db ordered by primary key column ascending.

Actual call delegated to QuerySet.

Passing args and/or kwargs is a shortcut and equals to calling filter(*args, **kwargs).first().

List of related models is cleared before the call.

Parameters:

Name Type Description Default
kwargs required

Returns:

Type Description
_asyncio.Future
Source code in ormar/relations/querysetproxy.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
async def first(self, *args: Any, **kwargs: Any) -> "T":
    """
    Gets the first row from the db ordered by primary key column ascending.

    Actual call delegated to QuerySet.

    Passing args and/or kwargs is a shortcut and equals to calling
    `filter(*args, **kwargs).first()`.

    List of related models is cleared before the call.

    :param kwargs:
    :type kwargs:
    :return:
    :rtype: _asyncio.Future
    """
    first = await self.queryset.first(*args, **kwargs)
    self._clean_items_on_load()
    self._register_related(first)
    return first

get(*args, **kwargs) async

Get's the first row from the db meeting the criteria set by kwargs.

If no criteria set it will return the last row in db sorted by pk.

Passing args and/or kwargs is a shortcut and equals to calling filter(*args, **kwargs).get().

Actual call delegated to QuerySet.

List of related models is cleared before the call.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
Model

returned model

Raises:

Type Description
NoMatch

if no rows are returned

MultipleMatches

if more than 1 row is returned.

Source code in ormar/relations/querysetproxy.py
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
async def get(self, *args: Any, **kwargs: Any) -> "T":
    """
    Get's the first row from the db meeting the criteria set by kwargs.

    If no criteria set it will return the last row in db sorted by pk.

    Passing args and/or kwargs is a shortcut and equals to calling
    `filter(*args, **kwargs).get()`.

    Actual call delegated to QuerySet.

    List of related models is cleared before the call.

    :raises NoMatch: if no rows are returned
    :raises MultipleMatches: if more than 1 row is returned.
    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: returned model
    :rtype: Model
    """
    get = await self.queryset.get(*args, **kwargs)
    self._clean_items_on_load()
    self._register_related(get)
    return get

get_or_create(_defaults=None, *args, **kwargs) async

Combination of create and get methods.

Tries to get a row meeting the criteria fro kwargs and if NoMatch exception is raised it creates a new one with given kwargs and _defaults.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required
_defaults Optional[Dict[str, Any]]

default values for creating object

None

Returns:

Type Description
Tuple("T", bool)

model instance and a boolean

Source code in ormar/relations/querysetproxy.py
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
async def get_or_create(
    self,
    _defaults: Optional[Dict[str, Any]] = None,
    *args: Any,
    **kwargs: Any,
) -> Tuple["T", bool]:
    """
    Combination of create and get methods.

    Tries to get a row meeting the criteria fro kwargs
    and if `NoMatch` exception is raised
    it creates a new one with given kwargs and _defaults.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :param _defaults: default values for creating object
    :type _defaults: Optional[Dict[str, Any]]
    :return: model instance and a boolean
    :rtype: Tuple("T", bool)
    """
    try:
        return await self.get(*args, **kwargs), False
    except NoMatch:
        _defaults = _defaults or {}
        return await self.create(**{**kwargs, **_defaults}), True

get_or_none(*args, **kwargs) async

Get's the first row from the db meeting the criteria set by kwargs.

If no criteria set it will return the last row in db sorted by pk.

Passing args and/or kwargs is a shortcut and equals to calling filter(*args, **kwargs).get_or_none().

If not match is found None will be returned.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
Model

returned model

Source code in ormar/relations/querysetproxy.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
async def get_or_none(self, *args: Any, **kwargs: Any) -> Optional["T"]:
    """
    Get's the first row from the db meeting the criteria set by kwargs.

    If no criteria set it will return the last row in db sorted by pk.

    Passing args and/or kwargs is a shortcut and equals to calling
    `filter(*args, **kwargs).get_or_none()`.

    If not match is found None will be returned.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: returned model
    :rtype: Model
    """
    try:
        get = await self.queryset.get(*args, **kwargs)
    except ormar.NoMatch:
        return None

    self._clean_items_on_load()
    self._register_related(get)
    return get

limit(limit_count)

You can limit the results to desired number of parent models.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
limit_count int

number of models to limit

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
def limit(self, limit_count: int) -> "QuerysetProxy[T]":
    """
    You can limit the results to desired number of parent models.

    Actual call delegated to QuerySet.

    :param limit_count: number of models to limit
    :type limit_count: int
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.limit(limit_count)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

max(columns) async

Returns max value of columns for rows matching the given criteria (applied with filter and exclude if set before).

Returns:

Type Description
Any

max value of column(s)

Source code in ormar/relations/querysetproxy.py
216
217
218
219
220
221
222
223
224
async def max(self, columns: Union[str, List[str]]) -> Any:  # noqa: A003
    """
    Returns max value of columns for rows matching the given criteria
    (applied with `filter` and `exclude` if set before).

    :return: max value of column(s)
    :rtype: Any
    """
    return await self.queryset.max(columns=columns)

min(columns) async

Returns min value of columns for rows matching the given criteria (applied with filter and exclude if set before).

Returns:

Type Description
Any

min value of column(s)

Source code in ormar/relations/querysetproxy.py
226
227
228
229
230
231
232
233
234
async def min(self, columns: Union[str, List[str]]) -> Any:  # noqa: A003
    """
    Returns min value of columns for rows matching the given criteria
    (applied with `filter` and `exclude` if set before).

    :return: min value of column(s)
    :rtype: Any
    """
    return await self.queryset.min(columns=columns)

offset(offset)

You can also offset the results by desired number of main models.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
offset int

numbers of models to offset

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
def offset(self, offset: int) -> "QuerysetProxy[T]":
    """
    You can also offset the results by desired number of main models.

    Actual call delegated to QuerySet.

    :param offset: numbers of models to offset
    :type offset: int
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.offset(offset)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

order_by(columns)

With order_by() you can order the results from database based on your choice of fields.

You can provide a string with field name or list of strings with fields names.

Ordering in sql will be applied in order of names you provide in order_by.

By default if you do not provide ordering ormar explicitly orders by all primary keys

If you are sorting by nested models that causes that the result rows are unsorted by the main model ormar will combine those children rows into one main model.

The main model will never duplicate in the result

To order by main model field just provide a field name

To sort on nested models separate field names with dunder '__'.

You can sort this way across all relation types -> ForeignKey, reverse virtual FK and ManyToMany fields.

To sort in descending order provide a hyphen in front of the field name

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
columns Union[List, str, 'OrderAction']

columns by which models should be sorted

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
def order_by(self, columns: Union[List, str, "OrderAction"]) -> "QuerysetProxy[T]":
    """
    With `order_by()` you can order the results from database based on your
    choice of fields.

    You can provide a string with field name or list of strings with fields names.

    Ordering in sql will be applied in order of names you provide in order_by.

    By default if you do not provide ordering `ormar` explicitly orders by
    all primary keys

    If you are sorting by nested models that causes that the result rows are
    unsorted by the main model `ormar` will combine those children rows into
    one main model.

    The main model will never duplicate in the result

    To order by main model field just provide a field name

    To sort on nested models separate field names with dunder '__'.

    You can sort this way across all relation types -> `ForeignKey`,
    reverse virtual FK and `ManyToMany` fields.

    To sort in descending order provide a hyphen in front of the field name

    Actual call delegated to QuerySet.

    :param columns: columns by which models should be sorted
    :type columns: Union[List, str]
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.order_by(columns)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

paginate(page, page_size=20)

You can paginate the result which is a combination of offset and limit clauses. Limit is set to page size and offset is set to (page-1) * page_size.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
page_size int

numbers of items per page

20
page int

page number

required

Returns:

Type Description
QuerySet

QuerySet

Source code in ormar/relations/querysetproxy.py
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
def paginate(self, page: int, page_size: int = 20) -> "QuerysetProxy[T]":
    """
    You can paginate the result which is a combination of offset and limit clauses.
    Limit is set to page size and offset is set to (page-1) * page_size.

    Actual call delegated to QuerySet.

    :param page_size: numbers of items per page
    :type page_size: int
    :param page: page number
    :type page: int
    :return: QuerySet
    :rtype: QuerySet
    """
    queryset = self.queryset.paginate(page=page, page_size=page_size)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

Allows to prefetch related models during query - but opposite to select_related each subsequent model is fetched in a separate database query.

With prefetch_related always one query per Model is run against the database, meaning that you will have multiple queries executed one after another.

To fetch related model use ForeignKey names.

To chain related Models relation use double underscores between names.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
related Union[List, str]

list of relation field names, can be linked by '__' to nest

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
def prefetch_related(self, related: Union[List, str]) -> "QuerysetProxy[T]":
    """
    Allows to prefetch related models during query - but opposite to
    `select_related` each subsequent model is fetched in a separate database query.

    **With `prefetch_related` always one query per Model is run against the
    database**, meaning that you will have multiple queries executed one
    after another.

    To fetch related model use `ForeignKey` names.

    To chain related `Models` relation use double underscores between names.

    Actual call delegated to QuerySet.

    :param related: list of relation field names, can be linked by '__' to nest
    :type related: Union[List, str]
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.prefetch_related(related)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

queryset() writable property

Returns queryset if it's set, AttributeError otherwise.

Returns:

Type Description
QuerySet

QuerySet

Source code in ormar/relations/querysetproxy.py
63
64
65
66
67
68
69
70
71
72
@property
def queryset(self) -> "QuerySet[T]":
    """
    Returns queryset if it's set, AttributeError otherwise.
    :return: QuerySet
    :rtype: QuerySet
    """
    if not self._queryset:
        raise AttributeError
    return self._queryset

select_all(follow=False)

By default adds only directly related models.

If follow=True is set it adds also related models of related models.

To not get stuck in an infinite loop as related models also keep a relation to parent model visited models set is kept.

That way already visited models that are nested are loaded, but the load do not follow them inside. So Model A -> Model B -> Model C -> Model A -> Model X will load second Model A but will never follow into Model X. Nested relations of those kind need to be loaded manually.

Parameters:

Name Type Description Default
follow bool

flag to trigger deep save - by default only directly related models are saved with follow=True also related models of related models are saved

False

Returns:

Type Description
Model

reloaded Model

Source code in ormar/relations/querysetproxy.py
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
def select_all(self, follow: bool = False) -> "QuerysetProxy[T]":
    """
    By default adds only directly related models.

    If follow=True is set it adds also related models of related models.

    To not get stuck in an infinite loop as related models also keep a relation
    to parent model visited models set is kept.

    That way already visited models that are nested are loaded, but the load do not
    follow them inside. So Model A -> Model B -> Model C -> Model A -> Model X
    will load second Model A but will never follow into Model X.
    Nested relations of those kind need to be loaded manually.

    :param follow: flag to trigger deep save -
    by default only directly related models are saved
    with follow=True also related models of related models are saved
    :type follow: bool
    :return: reloaded Model
    :rtype: Model
    """
    queryset = self.queryset.select_all(follow=follow)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

Allows to prefetch related models during the same query.

With select_related always only one query is run against the database, meaning that one (sometimes complicated) join is generated and later nested models are processed in python.

To fetch related model use ForeignKey names.

To chain related Models relation use double underscores between names.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
related Union[List, str]

list of relation field names, can be linked by '__' to nest

required

Returns:

Type Description
QuerysetProxy

QuerysetProxy

Source code in ormar/relations/querysetproxy.py
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
def select_related(self, related: Union[List, str]) -> "QuerysetProxy[T]":
    """
    Allows to prefetch related models during the same query.

    **With `select_related` always only one query is run against the database**,
    meaning that one (sometimes complicated) join is generated and later nested
    models are processed in python.

    To fetch related model use `ForeignKey` names.

    To chain related `Models` relation use double underscores between names.

    Actual call delegated to QuerySet.

    :param related: list of relation field names, can be linked by '__' to nest
    :type related: Union[List, str]
    :return: QuerysetProxy
    :rtype: QuerysetProxy
    """
    queryset = self.queryset.select_related(related)
    return self.__class__(
        relation=self.relation, type_=self.type_, to=self.to, qryset=queryset
    )

sum(columns) async

Returns sum value of columns for rows matching the given criteria (applied with filter and exclude if set before).

Returns:

Type Description
int

sum value of columns

Source code in ormar/relations/querysetproxy.py
236
237
238
239
240
241
242
243
244
async def sum(self, columns: Union[str, List[str]]) -> Any:  # noqa: A003
    """
    Returns sum value of columns for rows matching the given criteria
    (applied with `filter` and `exclude` if set before).

    :return: sum value of columns
    :rtype: int
    """
    return await self.queryset.sum(columns=columns)

update(each=False, **kwargs) async

Updates the model table after applying the filters from kwargs.

You have to either pass a filter to narrow down a query or explicitly pass each=True flag to affect whole table.

Parameters:

Name Type Description Default
each bool

flag if whole table should be affected if no filter is passed

False
kwargs Any

fields names and proper value types

required

Returns:

Type Description
int

number of updated rows

Source code in ormar/relations/querysetproxy.py
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
async def update(self, each: bool = False, **kwargs: Any) -> int:
    """
    Updates the model table after applying the filters from kwargs.

    You have to either pass a filter to narrow down a query or explicitly pass
    each=True flag to affect whole table.

    :param each: flag if whole table should be affected if no filter is passed
    :type each: bool
    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: number of updated rows
    :rtype: int
    """
    # queryset proxy always have one filter for pk of parent model
    if (
        not each
        and (len(self.queryset.filter_clauses) + len(self.queryset.exclude_clauses))
        == 1
    ):
        raise QueryDefinitionError(
            "You cannot update without filtering the queryset first. "
            "If you want to update all rows use update(each=True, **kwargs)"
        )

    through_kwargs = kwargs.pop(self.through_model_name, {})
    children = await self.queryset.all()
    for child in children:
        await child.update(**kwargs)  # type: ignore
        if self.type_ == ormar.RelationType.MULTIPLE and through_kwargs:
            await self.update_through_instance(
                child=child, **through_kwargs  # type: ignore
            )
    return len(children)

update_or_create(**kwargs) async

Updates the model, or in case there is no match in database creates a new one.

Actual call delegated to QuerySet.

Parameters:

Name Type Description Default
kwargs Any

fields names and proper value types

required

Returns:

Type Description
Model

updated or created model

Source code in ormar/relations/querysetproxy.py
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
async def update_or_create(self, **kwargs: Any) -> "T":
    """
    Updates the model, or in case there is no match in database creates a new one.

    Actual call delegated to QuerySet.

    :param kwargs: fields names and proper value types
    :type kwargs: Any
    :return: updated or created model
    :rtype: Model
    """
    pk_name = self.queryset.model_meta.pkname
    if "pk" in kwargs:
        kwargs[pk_name] = kwargs.pop("pk")
    if pk_name not in kwargs or kwargs.get(pk_name) is None:
        return await self.create(**kwargs)
    model = await self.queryset.get(pk=kwargs[pk_name])
    return await model.update(**kwargs)

update_through_instance(child, **kwargs) async

Updates a through model instance in the database for m2m relations.

Parameters:

Name Type Description Default
kwargs Any

dict of additional keyword arguments for through instance

required
child 'T'

child model instance

required
Source code in ormar/relations/querysetproxy.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
async def update_through_instance(self, child: "T", **kwargs: Any) -> None:
    """
    Updates a through model instance in the database for m2m relations.

    :param kwargs: dict of additional keyword arguments for through instance
    :type kwargs: Any
    :param child: child model instance
    :type child: Model
    """
    model_cls = self.relation.through
    owner_column = self.related_field.default_target_field_name()  # type: ignore
    child_column = self.related_field.default_source_field_name()  # type: ignore
    rel_kwargs = {owner_column: self._owner.pk, child_column: child.pk}
    through_model = await model_cls.objects.get(**rel_kwargs)
    await through_model.update(**kwargs)

upsert_through_instance(child, **kwargs) async

Updates a through model instance in the database for m2m relations if it already exists, else creates one.

Parameters:

Name Type Description Default
kwargs Any

dict of additional keyword arguments for through instance

required
child 'T'

child model instance

required
Source code in ormar/relations/querysetproxy.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
async def upsert_through_instance(self, child: "T", **kwargs: Any) -> None:
    """
    Updates a through model instance in the database for m2m relations if
    it already exists, else creates one.

    :param kwargs: dict of additional keyword arguments for through instance
    :type kwargs: Any
    :param child: child model instance
    :type child: Model
    """
    try:
        await self.update_through_instance(child=child, **kwargs)
    except NoMatch:
        await self.create_through_instance(child=child, **kwargs)

values(fields=None, exclude_through=False) async

Return a list of dictionaries with column values in order of the fields passed or all fields from queried models.

To filter for given row use filter/exclude methods before values, to limit number of rows use limit/offset or paginate before values.

Note that it always return a list even for one row from database.

Parameters:

Name Type Description Default
exclude_through bool

flag if through models should be excluded

False
fields Union[List, str, Set, Dict]

field name or list of field names to extract from db

None
Source code in ormar/relations/querysetproxy.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
async def values(
    self, fields: Union[List, str, Set, Dict] = None, exclude_through: bool = False
) -> List:
    """
    Return a list of dictionaries with column values in order of the fields
    passed or all fields from queried models.

    To filter for given row use filter/exclude methods before values,
    to limit number of rows use limit/offset or paginate before values.

    Note that it always return a list even for one row from database.

    :param exclude_through: flag if through models should be excluded
    :type exclude_through: bool
    :param fields: field name or list of field names to extract from db
    :type fields:  Union[List, str, Set, Dict]
    """
    return await self.queryset.values(
        fields=fields, exclude_through=exclude_through
    )

values_list(fields=None, flatten=False, exclude_through=False) async

Return a list of tuples with column values in order of the fields passed or all fields from queried models.

When one field is passed you can flatten the list of tuples into list of values of that single field.

To filter for given row use filter/exclude methods before values, to limit number of rows use limit/offset or paginate before values.

Note that it always return a list even for one row from database.

Parameters:

Name Type Description Default
exclude_through bool

flag if through models should be excluded

False
fields Union[List, str, Set, Dict]

field name or list of field names to extract from db

None
flatten bool

when one field is passed you can flatten the list of tuples

False
Source code in ormar/relations/querysetproxy.py
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
async def values_list(
    self,
    fields: Union[List, str, Set, Dict] = None,
    flatten: bool = False,
    exclude_through: bool = False,
) -> List:
    """
    Return a list of tuples with column values in order of the fields passed or
    all fields from queried models.

    When one field is passed you can flatten the list of tuples into list of values
    of that single field.

    To filter for given row use filter/exclude methods before values,
    to limit number of rows use limit/offset or paginate before values.

    Note that it always return a list even for one row from database.

    :param exclude_through: flag if through models should be excluded
    :type exclude_through: bool
    :param fields: field name or list of field names to extract from db
    :type fields: Union[str, List[str]]
    :param flatten: when one field is passed you can flatten the list of tuples
    :type flatten: bool
    """
    return await self.queryset.values(
        fields=fields,
        exclude_through=exclude_through,
        _as_dict=False,
        _flatten=flatten,
    )