Entity module¶
Quick Start¶
For a quick start and a high level description of the usage of this module, refer to Creating your Entity.
Module Documentation¶
Base entity for modeling of API’s entities
-
class
nova_api.entity.Entity(id_: str = <factory>, creation_datetime: datetime.datetime = <factory>, last_modified_datetime: datetime.datetime = <factory>)[source]¶ Base Entity implementation
This class is the base implementation of the entity for modeling API resources. The fields included in this class will be inherited for all subclasses.
Examples
@dataclass class Person(Entity): name: str = None age: int = None birthday: date = None
-
__iter__()[source]¶ Iteration through the Entity receiving the tuple (field_name, field_value_serialized)
Iterates through the fields in the entity and yields a tuple with the field_name and the serialized field_value. For fields that are instances of Entities, the key will have _id_ appended and the value will be the id_, as stated in _serialize_field.
Return key, value: The tuple with the field_name and field_value
-
__post_init__()[source]¶ Post processing of fields
Post init goes through the parameters passed to init and makes some validations. Fields that are subclasses of Entity will be instantiated (but only with id_ set). Datetime formats also will be cast if received as strings. Strings will have trailing and leading white spaces removed.
__post_init__ may be overridden to include validations required by any use case. In such case, it’s important to include a super call to maintain the aforementioned functionalities.
Returns: None
-
__weakref__¶ list of weak references to the object (if defined)
-
get_db_values(field_serializer=None) → list[source]¶ Returns all attributes to save in database with formatted values.
Goes through the fields in the entity and converts them to the expected value to save in the database. For example: datetime values are converted to string with the specific sql format.
Also verifies if a field contains in metadata database=False and excludes it from the list if so. Default for database is True.
Parameters: field_serializer – Custom function to define serialization of fields :return: Serialized values to save in database
-
static
serialize_field(field_value)[source]¶ Returns the value of a field formatted to be saved in the database.
For fields that are subclasses of Entity, returns only the id_ to save in the database. For datetime and date fields, values are transformed into strings with strftime function from datetime.
Parameters: field_value – Value of the field in the entity being serialized. Returns: The serialized value of the field for database insertion.
-