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[Manager[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.
asave
async
¶
asave(*, 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
adelete
async
¶
adelete(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,
context=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True,
serialize_as_any=False
)
Dumps the record and its references into a dictionary of data.
Args: mode (Literal['json', 'python'] | str, optional): The mode of serialization. Defaults to 'python'. include (IncEx, optional): Fields to include in the serialization. Defaults to None. exclude (IncEx, optional): Fields to exclude from the serialization. Defaults to None. context (Any | None, optional): The context of the serialization. Defaults to None. by_alias (bool, optional): Whether to use field aliases. Defaults to False. exclude_unset (bool, optional): Whether to exclude unset fields. Defaults to False. exclude_defaults (bool, optional): Whether to exclude fields with default values. Defaults to False. exclude_none (bool, optional): Whether to exclude fields with None values. Defaults to False. round_trip (bool, optional): Whether to ensure round-trip serialization. Defaults to False. warnings (bool, optional): Whether to show warnings. Defaults to True. serialize_as_any (bool, optional): Whether to serialize as Any. Defaults to False.
Returns: dict[str, Any]: A dictionary representation of the model.
model_dump ¶
model_dump(
*,
mode="python",
include=None,
exclude=None,
context=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True,
serialize_as_any=False
)
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, optional): The mode of serialization. Defaults to 'python'. include (IncEx, optional): Fields to include in the serialization. Defaults to None. exclude (IncEx, optional): Fields to exclude from the serialization. Defaults to None. context (Any | None, optional): The context of the serialization. Defaults to None. by_alias (bool, optional): Whether to use field aliases. Defaults to False. exclude_unset (bool, optional): Whether to exclude unset fields. Defaults to False. exclude_defaults (bool, optional): Whether to exclude fields with default values. Defaults to False. exclude_none (bool, optional): Whether to exclude fields with None values. Defaults to False. round_trip (bool, optional): Whether to ensure round-trip serialization. Defaults to False. warnings (bool, optional): Whether to show warnings. Defaults to True. serialize_as_any (bool, optional): Whether to serialize as Any. Defaults to False.
Returns: dict[str, Any]: A dictionary representation of the model.
model_dump_json_refs ¶
model_dump_json_refs(
*,
indent=None,
include=None,
exclude=None,
context=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True,
serialize_as_any=False
)
Similar to model_dump_refs
, but returns a JSON string instead of a dictionary.
Args: indent (int | None, optional): The indentation of the JSON string. Defaults to None. include (IncEx, optional): Fields to include in the serialization. Defaults to None. exclude (IncEx, optional): Fields to exclude from the serialization. Defaults to None. context (Any | None, optional): The context of the serialization. Defaults to None. by_alias (bool, optional): Whether to use field aliases. Defaults to False. exclude_unset (bool, optional): Whether to exclude unset fields. Defaults to False. exclude_defaults (bool, optional): Whether to exclude fields with default values. Defaults to False. exclude_none (bool, optional): Whether to exclude fields with None values. Defaults to False. round_trip (bool, optional): Whether to ensure round-trip serialization. Defaults to False. warnings (bool, optional): Whether to show warnings. Defaults to True. serialize_as_any (bool, optional): Whether to serialize as Any. Defaults to False.
Returns: str: A JSON string representation of the model.
model_dump_json ¶
model_dump_json(
*,
indent=None,
include=None,
exclude=None,
context=None,
by_alias=False,
exclude_unset=False,
exclude_defaults=False,
exclude_none=False,
round_trip=False,
warnings=True,
serialize_as_any=False
)
Similar to model_dump
, but returns a JSON string instead of a dictionary.
Args: mode (Literal['json', 'python'] | str, optional): The mode of serialization. Defaults to 'python'. include (IncEx, optional): Fields to include in the serialization. Defaults to None. exclude (IncEx, optional): Fields to exclude from the serialization. Defaults to None. context (Any | None, optional): The context of the serialization. Defaults to None. by_alias (bool, optional): Whether to use field aliases. Defaults to False. exclude_unset (bool, optional): Whether to exclude unset fields. Defaults to False. exclude_defaults (bool, optional): Whether to exclude fields with default values. Defaults to False. exclude_none (bool, optional): Whether to exclude fields with None values. Defaults to False. round_trip (bool, optional): Whether to ensure round-trip serialization. Defaults to False. warnings (bool, optional): Whether to show warnings. Defaults to True. serialize_as_any (bool, optional): Whether to serialize as Any. Defaults to False.
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.
aprevious_version
async
¶
aprevious_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.
anext_version
async
¶
anext_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(*, latest=False)
Gets the object with the current version from the database.
Returns: Self: The object with the current version from the database.
arefetch_from_db
async
¶
arefetch_from_db(*, latest=False)
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.
apre_create
async
¶
apre_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.
apost_create
async
¶
apost_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.
apre_update
async
¶
apre_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.
apost_update
async
¶
apost_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.
apre_delete
async
¶
apre_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.
apost_delete
async
¶
apost_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.