Skip to content

models.mixins.excludable_mixin

ExcludableMixin Objects

1
class ExcludableMixin(RelationMixin)

Used to include/exclude given set of fields on models during load and dict() calls.

get_child

1
2
 | @staticmethod
 | get_child(items: Union[Set, Dict, None], key: str = None) -> Union[Set, Dict, None]

Used to get nested dictionaries keys if they exists otherwise returns passed items.

Arguments:

  • items: bag of items to include or exclude :type items: Union[Set, Dict, None]
  • key: name of the child to extract :type key: str

Returns:

child extracted from items if exists :rtype: Union[Set, Dict, None]

_populate_pk_column

1
2
 | @staticmethod
 | _populate_pk_column(model: Union[Type["Model"], Type["ModelRow"]], columns: List[str], use_alias: bool = False) -> List[str]

Adds primary key column/alias (depends on use_alias flag) to list of column names that are selected.

Arguments:

  • model: model on columns are selected :type model: Type["Model"]
  • columns: list of columns names :type columns: List[str]
  • use_alias: flag to set if aliases or field names should be used :type use_alias: bool

Returns:

list of columns names with pk column in it :rtype: List[str]

own_table_columns

1
2
 | @classmethod
 | own_table_columns(cls, model: Union[Type["Model"], Type["ModelRow"]], excludable: ExcludableItems, alias: str = "", use_alias: bool = False, add_pk_columns: bool = True) -> List[str]

Returns list of aliases or field names for given model. Aliases/names switch is use_alias flag.

If provided only fields included in fields will be returned. If provided fields in exclude_fields will be excluded in return.

Primary key field is always added and cannot be excluded (will be added anyway).

Arguments:

  • add_pk_columns: flag if add primary key - always yes if ormar parses data :type add_pk_columns: bool
  • alias: relation prefix :type alias: str
  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems
  • model: model on columns are selected :type model: Type["Model"]
  • use_alias: flag if aliases or field names should be used :type use_alias: bool

Returns:

list of column field names or aliases :rtype: List[str]

1
2
 | @classmethod
 | _update_excluded_with_related(cls, exclude: Union[Set, Dict, None]) -> Set

Used during generation of the dict(). To avoid cyclical references and max recurrence limit nested models have to exclude related models that are not mandatory.

For a main model (not nested) only nullable related field names are added to exclusion, for nested models all related models are excluded.

Arguments:

  • exclude: set/dict with fields to exclude :type exclude: Union[Set, Dict, None]

Returns:

set or dict with excluded fields added. :rtype: Union[Set, Dict]

_update_excluded_with_pks_and_through

1
2
 | @classmethod
 | _update_excluded_with_pks_and_through(cls, exclude: Set, exclude_primary_keys: bool, exclude_through_models: bool) -> Set

Updates excluded names with name of pk column if exclude flag is set.

Arguments:

  • exclude: set of names to exclude :type exclude: Set
  • exclude_primary_keys: flag if the primary keys should be excluded :type exclude_primary_keys: bool

Returns:

set updated with pk if flag is set :rtype: Set

get_names_to_exclude

1
2
 | @classmethod
 | get_names_to_exclude(cls, excludable: ExcludableItems, alias: str) -> Set

Returns a set of models field names that should be explicitly excluded during model initialization.

Those fields will be set to None to avoid ormar/pydantic setting default values on them. They should be returned as None in any case.

Used in parsing data from database rows that construct Models by initializing them with dicts constructed from those db rows.

Arguments:

  • alias: alias of current relation :type alias: str
  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems

Returns:

set of field names that should be excluded :rtype: Set