Model
AMSDAL's Base model class
amsdal_models.classes.model.Model ¶
Bases: TypeModel
Base class for all model classes.
Attributes: model_config (ConfigDict): Configuration for the model. objects (ClassVar[BaseManager[Self]]): Manager for the Model class.
save ¶
save(*, force_insert=False, using=None, skip_hooks=False)
Saves the record of the Model object into the database.
By default, the object will be updated in the database if it already exists.
If force_insert
is set to True, the object will be inserted into the database even if it already exists,
which may result in an ObjectAlreadyExistsError
.
The method first checks if force_insert
is True, and if the object already exists in the database.
If it does, it raises an ObjectAlreadyExistsError
.
Then, depending on the object existence, the method either creates a new record in the database or updates
the existing record. It also triggers the corresponding pre_create()
, post_create()
, pre_update()
, and
post_update()
hooks.
Finally, the method returns the saved Model object.
Args: force_insert (bool): Indicates whether to force insert the object into the database, even if it already exists. using (str | None): The name of the database to use. skip_hooks (bool): Indicates whether to skip the hooks.
Returns: Model: The saved Model object.
delete ¶
delete(using=None, *, skip_hooks=False)
Deletes the existing record of the Model object from the database.
This method first calls the pre_delete()
method, then deletes the record from the database by calling
the _delete()
method, and finally calls the post_delete()
method.
It changes the flag is_deleted
to True in the metadata of the record.
Args:
using (str | None): The name of the database to use.
skip_hooks (bool): Indicates whether to skip the pre_delete()
and post_delete()
hooks.
Returns: None
display_name
property
¶
display_name
Gets the display name of the Model object.
This method returns the string representation of the object's address.
Returns: str: The display name of the Model object.
model_dump_refs ¶
model_dump_refs(
*,
mode="python",
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True
)
Dumps the record and its references into a dictionary of data.
Args:
mode (Literal['json', 'python'] | str): The mode in which to_python
should run. If mode is 'json',
the dictionary will only contain JSON serializable types. If mode is 'python',
the dictionary may contain any Python objects.
include (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to include
in the output.
exclude (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to exclude from
the output.
by_alias (bool): Whether to use the field's alias in the dictionary key if defined.
exclude_unset (bool): Whether to exclude fields that are unset or None from the output.
exclude_defaults (bool): Whether to exclude fields that are set to their default value from the output.
exclude_none (bool): Whether to exclude fields that have a value of None
from the output.
round_trip (bool): Whether to enable serialization and deserialization round-trip support.
warnings (bool): Whether to log warnings when invalid fields are encountered.
Returns: dict[str, Any]: A dictionary representation of the model.
model_dump ¶
model_dump(
*,
mode="python",
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True
)
This method is used to dump the record dictionary of data, although the referenced objects will be represented in reference format. Here is an example of reference format:
{
"$ref": {
"resource": "sqlite",
"class_name": "Person",
"class_version": "1234",
"object_id": "4567",
"object_version": "8901"
}
}
Args:
mode (Literal['json', 'python'] | str): The mode in which to_python
should run. If mode is 'json', the
dictionary will only contain JSON serializable types. If mode is 'python', the dictionary may contain
any Python objects.
include (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to include
in the output.
exclude (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to exclude from
the output.
by_alias (bool): Whether to use the field's alias in the dictionary key if defined.
exclude_unset (bool): Whether to exclude fields that are unset or None from the output.
exclude_defaults (bool): Whether to exclude fields that are set to their default value from the output.
exclude_none (bool): Whether to exclude fields that have a value of None
from the output.
round_trip (bool): Whether to enable serialization and deserialization round-trip support.
warnings (bool): Whether to log warnings when invalid fields are encountered.
Returns: dict[str, Any]: A dictionary representation of the model.
model_dump_json_refs ¶
model_dump_json_refs(
*,
indent=None,
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True
)
Similar to model_dump_refs
, but returns a JSON string instead of a dictionary.
Args:
indent (int | None): Indentation to use in the JSON output. If None is passed, the output will be compact.
include (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to include
in the output.
exclude (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to exclude from
the output.
by_alias (bool): Whether to use the field's alias in the dictionary key if defined.
exclude_unset (bool): Whether to exclude fields that are unset or None from the output.
exclude_defaults (bool): Whether to exclude fields that are set to their default value from the output.
exclude_none (bool): Whether to exclude fields that have a value of None
from the output.
round_trip (bool): Whether to enable serialization and deserialization round-trip support.
warnings (bool): Whether to log warnings when invalid fields are encountered.
Returns: str: A JSON string representation of the model.
model_dump_json ¶
model_dump_json(
*,
indent=None,
include=None,
exclude=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True
)
Similar to model_dump
, but returns a JSON string instead of a dictionary.
Args:
indent (int | None): Indentation to use in the JSON output. If None is passed, the output will be compact.
include (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to include
in the output.
exclude (set[int] | set[str] | dict[int, Any] | dict[str, Any] | None): A list of fields to exclude from
the output.
by_alias (bool): Whether to use the field's alias in the dictionary key if defined.
exclude_unset (bool): Whether to exclude fields that are unset or None from the output.
exclude_defaults (bool): Whether to exclude fields that are set to their default value from the output.
exclude_none (bool): Whether to exclude fields that have a value of None
from the output.
round_trip (bool): Whether to enable serialization and deserialization round-trip support.
warnings (bool): Whether to log warnings when invalid fields are encountered.
Returns: str: A JSON string representation of the model.
previous_version ¶
previous_version()
Gets the previous version of the Model object from the database.
This method returns the Model object that is the previous version of the current object, if it exists. Otherwise, it returns None.
Returns: Self | None: The previous version of the Model object.
next_version ¶
next_version()
Gets the next version of the Model object from the database.
This method returns the Model object that is the next version of the current object, if it exists. Otherwise, it returns None.
Returns: Self | None: The next version of the Model object.
refetch_from_db ¶
refetch_from_db()
Gets the object with the current version from the database.
Returns: Self: The object with the current version from the database.
amsdal_models.classes.mixins.model_hooks_mixin.ModelHooksMixin ¶
pre_init ¶
pre_init(*, is_new_object, kwargs)
This hook is called just before the model is initialized and the built-in validations are executed. This hook is useful for setting default values for the model fields or for validating the keyword arguments.
Args: is_new_object (bool): Indicates if the model is being initialized as a new record or existing one. kwargs (dict[str, Any]): Dictionary of keyword arguments passed to the model constructor.
Returns: None: Does not return anything.
post_init ¶
post_init(*, is_new_object, kwargs)
This hook is called just after the model is initialized and the built-in validations are executed. This hook also is useful for setting default values for the model fields or for extra validating the keyword arguments.
Args: is_new_object (bool): Indicates if the model is being initialized as a new record or existing one. kwargs (dict[str, Any]): Dictionary of keyword arguments passed to the model constructor.
Returns: None: Does not return anything.
pre_create ¶
pre_create()
This hook is called just before the new record of the model is saved to the database. It doesn't accept any arguments. This hook is useful for setting default values for the model fields or for validating the model before saving it to the database. At this stage, the object is not saved to the database yet.
Returns: None: Does not return anything.
post_create ¶
post_create()
This hook is called just after the new record of the model is saved to the database. It doesn't accept any arguments. This hook is useful for adding extra logic after the model is saved to the database. For example, you can send a notification to the user that the new record is created. At this stage, the object is already saved to the database and can be referenced by other records.
Returns: None: Does not return anything.
pre_update ¶
pre_update()
This hook is called just before the existing record of the model is updated in the database. It doesn't accept any arguments. This hook is useful for validating the model before updating it in the database. At this stage, the object is not updated in the database yet, so previous_version may return None.
Returns: None: Does not return anything.
post_update ¶
post_update()
This hook is called just after the existing record of the model is updated in the database. It doesn't accept any arguments. This hook is useful for adding extra logic after the model is updated in the database. For example, you can send a notification to the user that the record is updated. At this stage, the object is already updated in the database, and calling previous_version will return the version of the object before the update.
Returns: None: Does not return anything.
pre_delete ¶
pre_delete()
This hook is called just before the existing record of the model is deleted from the database. It doesn't accept any arguments. This hook is useful for validating the model before deleting it from the database. At this stage, the object is not deleted from the database yet.
Returns: None: Does not return anything.
post_delete ¶
post_delete()
This hook is called just after the existing record of the model is deleted from the database. It doesn't accept any arguments. This hook is useful for adding extra logic after the model is deleted from the database. For example, you can send a notification to the user that the record is deleted.
Returns: None: Does not return anything.