15 KiB
models.newbasemodel
NewBaseModel Objects
class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass)
Main base class of ormar Model. Inherits from pydantic BaseModel and has all mixins combined in ModelTableProxy. Constructed with ModelMetaclass which in turn also inherits pydantic metaclass.
Abstracts away all internals and helper functions, so final Model class has only the logic concerned with database connection and data persistance.
__init__
| __init__(*args: Any, **kwargs: Any) -> None
Initializer that creates a new ormar Model that is also pydantic Model at the same time.
Passed keyword arguments can be only field names and their corresponding values as those will be passed to pydantic validation that will complain if extra params are passed.
If relations are defined each relation is expanded and children models are also initialized and validated. Relation from both sides is registered so you can access related models from both sides.
Json fields are automatically loaded/dumped if needed.
Models marked as abstract=True in internal Meta class cannot be initialized.
Accepts also special pk_only flag that indicates that Model is constructed only with primary key value (so no other fields, it's a child model on other Model), that causes skipping the validation, that's the only case when the validation can be skipped.
Accepts also special excluded parameter that contains a set of fields that should be explicitly set to None, as otherwise pydantic will try to populate them with their default values if default is set.
Raises:
ModelError: if abstract model is initialized, model has ForwardRefs that has not been updated or unknown field is passed
Arguments:
args(Any): ignored argskwargs(Any): keyword arguments - all fields values and some special params
__setattr__
| __setattr__(name: str, value: Any) -> None
Overwrites setattr in pydantic parent as otherwise descriptors are not called.
Arguments:
name(str): name of the attribute to setvalue(Any): value of the attribute to set
Returns:
None: None
__getattr__
| __getattr__(item: str) -> Any
Used only to silence mypy errors for Through models and reverse relations. Not used in real life as in practice calls are intercepted by RelationDescriptors
Arguments:
item(str): name of attribute
Returns:
Any: Any
_internal_set
| _internal_set(name: str, value: Any) -> None
Delegates call to pydantic.
Arguments:
name(str): name of paramvalue(Any): value to set
_verify_model_can_be_initialized
| _verify_model_can_be_initialized() -> None
Raises exception if model is abstract or has ForwardRefs in relation fields.
Returns:
None: None
_process_kwargs
| _process_kwargs(kwargs: Dict) -> Tuple[Dict, Dict]
Initializes nested models.
Removes property_fields
Checks if field is in the model fields or pydatnic fields.
Nullifies fields that should be excluded.
Extracts through models from kwargs into temporary dict.
Arguments:
kwargs(Dict): passed to init keyword arguments
Returns:
Tuple[Dict, Dict]: modified kwargs
_initialize_internal_attributes
| _initialize_internal_attributes() -> None
Initializes internal attributes during init()
Returns:
None:
__eq__
| __eq__(other: object) -> bool
Compares other model to this model. when == is called.
Arguments:
other(object): other model to compare
Returns:
bool: result of comparison
__same__
| __same__(other: "NewBaseModel") -> bool
Used by eq, compares other model to this model. Compares:
- _orm_ids,
- primary key values if it's set
- dictionary of own fields (excluding relations)
Arguments:
other(NewBaseModel): model to compare to
Returns:
bool: result of comparison
get_name
| @classmethod
| get_name(cls, lower: bool = True) -> str
Returns name of the Model class, by default lowercase.
Arguments:
lower(bool): flag if name should be set to lowercase
Returns:
str: name of the model
pk_column
| @property
| pk_column() -> sqlalchemy.Column
Retrieves primary key sqlalchemy column from models Meta.table. Each model has to have primary key. Only one primary key column is allowed.
Returns:
sqlalchemy.Column: primary key sqlalchemy column
saved
| @property
| saved() -> bool
Saved status of the model. Changed by setattr and loading from db
signals
| @property
| signals() -> "SignalEmitter"
Exposes signals from model Meta
pk_type
| @classmethod
| pk_type(cls) -> Any
Shortcut to models primary key field type
db_backend_name
| @classmethod
| db_backend_name(cls) -> str
Shortcut to database dialect, cause some dialect require different treatment
remove
| remove(parent: "Model", name: str) -> None
Removes child from relation with given name in RelationshipManager
set_save_status
| set_save_status(status: bool) -> None
Sets value of the save status
get_properties
| @classmethod
| get_properties(cls, include: Union[Set, Dict, None], exclude: Union[Set, Dict, None]) -> Set[str]
Returns a set of names of functions/fields decorated with @property_field decorator.
They are added to dictionary when called directly and therefore also are present in fastapi responses.
Arguments:
include(Union[Set, Dict, None]): fields to includeexclude(Union[Set, Dict, None]): fields to exclude
Returns:
Set[str]: set of property fields names
update_forward_refs
| @classmethod
| update_forward_refs(cls, **localns: Any) -> None
Processes fields that are ForwardRef and need to be evaluated into actual models.
Expands relationships, register relation in alias manager and substitutes sqlalchemy columns with new ones with proper column type (null before).
Populates Meta table of the Model which is left empty before.
Sets self_reference flag on models that links to themselves.
Calls the pydantic method to evaluate pydantic fields.
Arguments:
localns(Any): local namespace
Returns:
None: None
_get_not_excluded_fields
| @staticmethod
| _get_not_excluded_fields(fields: Union[List, Set], include: Optional[Dict], exclude: Optional[Dict]) -> List
Returns related field names applying on them include and exclude set.
Arguments:
include(Union[Set, Dict, None]): fields to includeexclude(Union[Set, Dict, None]): fields to exclude
Returns:
List of fields with relations that is not excluded:
_extract_nested_models_from_list
| @staticmethod
| _extract_nested_models_from_list(relation_map: Dict, models: MutableSequence, include: Union[Set, Dict, None], exclude: Union[Set, Dict, None], exclude_primary_keys: bool, exclude_through_models: bool) -> List
Converts list of models into list of dictionaries.
Arguments:
models(List): List of modelsinclude(Union[Set, Dict, None]): fields to includeexclude(Union[Set, Dict, None]): fields to exclude
Returns:
List[Dict]: list of models converted to dictionaries
_skip_ellipsis
| @classmethod
| _skip_ellipsis(cls, items: Union[Set, Dict, None], key: str, default_return: Any = None) -> Union[Set, Dict, None]
Helper to traverse the include/exclude dictionaries. In dict() Ellipsis should be skipped as it indicates all fields required and not the actual set/dict with fields names.
Arguments:
items(Union[Set, Dict, None]): current include/exclude valuekey(str): key for nested relations to check
Returns:
Union[Set, Dict, None]: nested value of the items
_convert_all
| @staticmethod
| _convert_all(items: Union[Set, Dict, None]) -> Union[Set, Dict, None]
Helper to convert all pydantic special index to ormar which does not support index based exclusions.
Arguments:
items(Union[Set, Dict, None]): current include/exclude value
_extract_nested_models
| _extract_nested_models(relation_map: Dict, dict_instance: Dict, include: Optional[Dict], exclude: Optional[Dict], exclude_primary_keys: bool, exclude_through_models: bool) -> Dict
Traverse nested models and converts them into dictionaries. Calls itself recursively if needed.
Arguments:
nested(bool): flag if current instance is nesteddict_instance(Dict): current instance dictinclude(Optional[Dict]): fields to includeexclude(Optional[Dict]): fields to exclude
Returns:
Dict: current model dict with child models converted to dictionaries
dict
| dict(*, include: Union[Set, Dict] = None, exclude: Union[Set, Dict] = None, by_alias: bool = False, skip_defaults: bool = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, exclude_primary_keys: bool = False, exclude_through_models: bool = False, relation_map: Dict = None) -> "DictStrAny"
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
Nested models are also parsed to dictionaries.
Additionally fields decorated with @property_field are also added.
Arguments:
exclude_through_models(bool): flag to exclude through models from dictexclude_primary_keys(bool): flag to exclude primary keys from dictinclude(Union[Set, Dict, None]): fields to includeexclude(Union[Set, Dict, None]): fields to excludeby_alias(bool): flag to get values by alias - passed to pydanticskip_defaults(bool): flag to not set values - passed to pydanticexclude_unset(bool): flag to exclude not set values - passed to pydanticexclude_defaults(bool): flag to exclude default values - passed to pydanticexclude_none(bool): flag to exclude None values - passed to pydanticrelation_map(Dict): map of the relations to follow to avoid circural deps
Returns:
json
| json(*, include: Union[Set, Dict] = None, exclude: Union[Set, Dict] = None, by_alias: bool = False, skip_defaults: bool = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Optional[Callable[[Any], Any]] = None, exclude_primary_keys: bool = False, exclude_through_models: bool = False, **dumps_kwargs: Any, ,) -> str
Generate a JSON representation of the model, include and exclude
arguments as per dict().
encoder is an optional function to supply as default to json.dumps(),
other arguments as per json.dumps().
update_from_dict
| update_from_dict(value_dict: Dict) -> "NewBaseModel"
Updates self with values of fields passed in the dictionary.
Arguments:
value_dict(Dict): dictionary of fields names and values
Returns:
NewBaseModel: self
_convert_to_bytes
| _convert_to_bytes(column_name: str, value: Any) -> Union[str, Dict]
Converts value to bytes from string
Arguments:
column_name(str): name of the fieldvalue(Any): value fo the field
Returns:
Any: converted value if needed, else original value
_convert_bytes_to_str
| _convert_bytes_to_str(column_name: str, value: Any) -> Union[str, Dict]
Converts value to str from bytes for represent_as_base64_str columns.
Arguments:
column_name(str): name of the fieldvalue(Any): value fo the field
Returns:
Any: converted value if needed, else original value
_convert_json
| _convert_json(column_name: str, value: Any) -> Union[str, Dict]
Converts value to/from json if needed (for Json columns).
Arguments:
column_name(str): name of the fieldvalue(Any): value fo the field
Returns:
Any: converted value if needed, else original value
_extract_own_model_fields
| _extract_own_model_fields() -> Dict
Returns a dictionary with field names and values for fields that are not relations fields (ForeignKey, ManyToMany etc.)
Returns:
Dict: dictionary of fields names and values.
_extract_model_db_fields
| _extract_model_db_fields() -> Dict
Returns a dictionary with field names and values for fields that are stored in current model's table.
That includes own non-relational fields ang foreign key fields.
Returns:
Dict: dictionary of fields names and values.
get_relation_model_id
| get_relation_model_id(target_field: "BaseField") -> Optional[int]
Returns an id of the relation side model to use in prefetch query.
Arguments:
target_field("BaseField"): field with relation definition
Returns:
Optional[int]: value of pk if set