Skip to content

models.mixins.save_mixin

SavePrepareMixin Objects

1
class SavePrepareMixin(RelationMixin,  AliasMixin)

Used to prepare models to be saved in database

prepare_model_to_save

1
2
 | @classmethod
 | prepare_model_to_save(cls, new_kwargs: dict) -> dict

Combines all preparation methods before saving. Removes primary key for if it's nullable or autoincrement pk field, and it's set to None. Substitute related models with their primary key values as fk column. Populates the default values for field with default set and no value. Translate columns into aliases (db names).

Arguments:

  • new_kwargs: dictionary of model that is about to be saved :type new_kwargs: Dict[str, str]

Returns:

dictionary of model that is about to be saved :rtype: Dict[str, str]

_remove_not_ormar_fields

1
2
 | @classmethod
 | _remove_not_ormar_fields(cls, new_kwargs: dict) -> dict

Removes primary key for if it's nullable or autoincrement pk field, and it's set to None.

Arguments:

  • new_kwargs: dictionary of model that is about to be saved :type new_kwargs: Dict[str, str]

Returns:

dictionary of model that is about to be saved :rtype: Dict[str, str]

_remove_pk_from_kwargs

1
2
 | @classmethod
 | _remove_pk_from_kwargs(cls, new_kwargs: dict) -> dict

Removes primary key for if it's nullable or autoincrement pk field, and it's set to None.

Arguments:

  • new_kwargs: dictionary of model that is about to be saved :type new_kwargs: Dict[str, str]

Returns:

dictionary of model that is about to be saved :rtype: Dict[str, str]

parse_non_db_fields

1
2
 | @classmethod
 | parse_non_db_fields(cls, model_dict: Dict) -> Dict

Receives dictionary of model that is about to be saved and changes uuid fields to strings in bulk_update.

Arguments:

  • model_dict: dictionary of model that is about to be saved :type model_dict: Dict

Returns:

dictionary of model that is about to be saved :rtype: Dict

substitute_models_with_pks

1
2
 | @classmethod
 | substitute_models_with_pks(cls, model_dict: Dict) -> Dict

Receives dictionary of model that is about to be saved and changes all related models that are stored as foreign keys to their fk value.

Arguments:

  • model_dict: dictionary of model that is about to be saved :type model_dict: Dict

Returns:

dictionary of model that is about to be saved :rtype: Dict

populate_default_values

1
2
 | @classmethod
 | populate_default_values(cls, new_kwargs: Dict) -> Dict

Receives dictionary of model that is about to be saved and populates the default value on the fields that have the default value set, but no actual value was passed by the user.

Arguments:

  • new_kwargs: dictionary of model that is about to be saved :type new_kwargs: Dict

Returns:

dictionary of model that is about to be saved :rtype: Dict

validate_choices

1
2
 | @classmethod
 | validate_choices(cls, new_kwargs: Dict) -> Dict

Receives dictionary of model that is about to be saved and validates the fields with choices set to see if the value is allowed.

Arguments:

  • new_kwargs: dictionary of model that is about to be saved :type new_kwargs: Dict

Returns:

dictionary of model that is about to be saved :rtype: Dict

_upsert_model

1
2
 | @staticmethod
 | async _upsert_model(instance: "Model", save_all: bool, previous_model: Optional["Model"], relation_field: Optional["ForeignKeyField"], update_count: int) -> int

Method updates given instance if:

  • instance is not saved or
  • instance have no pk or
  • save_all=True flag is set

and instance is not pk_only.

If relation leading to instance is a ManyToMany also the through model is saved

Arguments:

  • instance: current model to upsert :type instance: Model
  • save_all: flag if all models should be saved or only not saved ones :type save_all: bool
  • relation_field: field with relation :type relation_field: Optional[ForeignKeyField]
  • previous_model: previous model from which method came :type previous_model: Model
  • update_count: no of updated models :type update_count: int

Returns:

no of updated models :rtype: int

_upsert_through_model

1
2
 | @staticmethod
 | async _upsert_through_model(instance: "Model", previous_model: "Model", relation_field: "ForeignKeyField") -> None

Upsert through model for m2m relation.

Arguments:

  • instance: current model to upsert :type instance: Model
  • relation_field: field with relation :type relation_field: Optional[ForeignKeyField]
  • previous_model: previous model from which method came :type previous_model: Model

_update_relation_list

1
 | async _update_relation_list(fields_list: Collection["ForeignKeyField"], follow: bool, save_all: bool, relation_map: Dict, update_count: int) -> int

Internal method used in save_related to follow deeper from related models and update numbers of updated related instances.

:type save_all: flag if all models should be saved :type save_all: bool

Arguments:

  • fields_list: list of ormar fields to follow and save :type fields_list: Collection["ForeignKeyField"]
  • relation_map: map of relations to follow :type relation_map: Dict
  • 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
  • update_count: internal parameter for recursive calls - number of updated instances :type update_count: int

Returns:

tuple of update count and visited :rtype: int

_get_field_values

1
 | _get_field_values(name: str) -> List

Extract field values and ensures it is a list.

Arguments:

  • name: name of the field :type name: str

Returns:

list of values :rtype: List