MCPcopy
hub / github.com/ormar-orm/ormar / fields

Method fields

ormar/queryset/queryset.py:532–590  ·  view source on GitHub ↗

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 n

(
        self, columns: Union[list, str, set, dict], slot: "Slot" = "include"
    )

Source from the content-addressed store, hash-verified

530 return select_paths, prefetch_paths
531
532 def fields(
533 self, columns: Union[list, str, set, dict], slot: "Slot" = "include"
534 ) -> "QuerySet[T]":
535 """
536 With `fields()` you can select subset of model columns to limit the data load.
537
538 Note that `fields()` and `exclude_fields()` works both for main models
539 (on normal queries like `get`, `all` etc.)
540 as well as `select_related` and `prefetch_related`
541 models (with nested notation).
542
543 You can select specified fields by passing a `str, list[str], set[str] or
544 dict` with nested definition.
545
546 To include related models use notation
547 `{related_name}__{column}[__{optional_next} etc.]`.
548
549 `fields()` can be called several times, building up the columns to select.
550
551 If you include related models into `select_related()` call but you won't specify
552 columns for those models in fields - implies a list of all fields for
553 those nested models.
554
555 Mandatory fields cannot be excluded as it will raise `ValidationError`,
556 to exclude a field it has to be nullable.
557
558 Pk column cannot be excluded - it's always auto added even if
559 not explicitly included.
560
561 You can also pass fields to include as dictionary or set.
562
563 To mark a field as included in a dictionary use it's name as key
564 and ellipsis as value.
565
566 To traverse nested models use nested dictionaries.
567
568 To include fields at last level instead of nested dictionary a set can be used.
569
570 To include whole nested model specify model related field name and ellipsis.
571
572 :param slot: which Excludable slot to write into ("include" or "exclude")
573 :type slot: Slot
574 :param columns: columns to include
575 :type columns: Union[list, str, set, dict]
576 :return: QuerySet
577 :rtype: QuerySet
578 """
579 excludable = ormar.ExcludableItems.from_excludable(self._excludable)
580 excludable.build(
581 items=columns,
582 model_cls=self.model_cls, # type: ignore
583 slot=slot,
584 )
585 if excludable._flatten_paths:
586 excludable.validate_flatten_vs_excludable(
587 source_model=self.model_cls # type: ignore
588 )
589

Calls 4

rebuild_selfMethod · 0.95
from_excludableMethod · 0.80
buildMethod · 0.80